Forums / General / How to test for existance of variable

How to test for existance of variable

Author Message

/dev/null

Friday 29 December 2006 12:40:02 am

<b>Question</b>

How would one test for existence of a template variable, or template object item ($module_result.node_id) without triggering template error, warning or notice when the 'node_id' does not exist?

I am tuning all my custom templates for performance. I am finding errors of this nature are accumulating in my custom templates. I have not yet seen how to avoid this problem.

<b>Example - Error</b>

Error: eZTemplate @ extension/example_com/design/example_com/templates/page_head.tpl:30[7]  	Dec 29 2006 03:10:56

No such attribute for array(7): node_id
Choose one of following: content, is_default_navigation_part, navigation_part,
                         ui_context, ui_component, path, uri

<b>Example - Template</b>
file, 'extension/example_com/design/example_com/templates/page_head.tpl'

<title>{if $module_result.node_id|eq(2)}{$site.title|wash}: {$site.description|wash} : Home{else}{$site_title}{/if}</title>

<b>Example - Url/Uri</b>
http://example.com/shop/checkout

<i>/dev/null</i>

eZpedia community documentation project: http://ezpedia.org

Kristof Coomans

Friday 29 December 2006 1:12:36 am

Hi /dev/null

You can use the is_set operator: http://ez.no/doc/ez_publish/technical_manual/3_8/reference/template_operators/variable_and_type_handling/is_set

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

/dev/null

Friday 29 December 2006 1:19:18 am

That Worked Great!

Many Thanks!
<i>/dev/null</i>

eZpedia community documentation project: http://ezpedia.org

Kristof Coomans

Friday 29 December 2006 2:51:07 am

I added a page to eZpedia which makes it easier for people familiar with PHP to switch to the template language: http://ezpedia.org/wiki/en/ez/template_language_for_php_developers

I'm not sure this helps you /dev/null. If you are familiar with other template/scripting/programming languages, feel free to add your own page on eZpedia to compare them with the eZ template language.

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

/dev/null

Friday 29 December 2006 3:30:45 am

Kristof!

That is a very fine page! <i>I know I have pine'd for such a page of documentation on several occasions in the past. Thank you</i>

Perhaps entries could be added for asp.net, cfml, perl, sql, ruby, python by others if needed, which would be the first language after php to add an entry for??

<i>/dev/null</i>

eZpedia community documentation project: http://ezpedia.org

Kristof Coomans

Friday 29 December 2006 3:38:20 am

sql seems rather strange to compare with the template language ;-)

independent eZ Publish developer and service provider | http://blog.coomanskristof.be | http://ezpedia.org

David Boman

Friday 29 December 2006 7:28:08 am

I agree with /dev/null - this is a very good page and I know for sure that I will use it alot when i code templates! Thanks!