If statements in templates

Author Message

Mike Cohen

Thursday 16 January 2003 4:51:47 pm

Is there some way to use an if-else type statement in the template files?

For example to only show a Heading if there is actually content under the heading. I've tried setting:

{let list_count=fetch('content','list_count',hash(parent_node_id,$node.node_id))}

Then wrapping the conditional text in a {section} block with the following types of {section} code, but I really don't know what they mean or even if I'm on the right track.
{section show=$list_count}
{section show=eq($list_count)}
{section show=and($list_count)}

Also wanted to know if there is a way to set and display a count variable within a loop.

like this:

{let count=0}
{section loop code here}
$count
{let count=$count+1}
{/section}

Thanks,

Mike

sergey podlesnyi

Friday 17 January 2003 4:05:32 am

> Is there some way to use an if-else type statement in the
> template files?
>
> For example to only show a Heading if there is actually
> content under the heading. I've tried setting:
>
> {let
> list_count=fetch('content','list_count',hash(parent_node_id,$node.node_id))}
>
> Then wrapping the conditional text in a {section} block with
> the following types of {section} code, but I really don't
> know what they mean or even if I'm on the right track.
> {section show=$list_count}
> {section show=eq($list_count)}
> {section show=and($list_count)}
>

You are right, you may this syntax:

{section show=gt($list_count,0)}
...if ther is some content ...
{section-else}
...if there is no content...
{/section}

obviously, operators and, not, or, gt, eq are normal logical operators, just taking operands inside brackets.

In case you doubt in what meaning your variable takes print debug information for yourself, just to know to what value to compare:

{$list_count} will print its value on output HTML page.

> Also wanted to know if there is a way to set and display a
> count variable within a loop.
>
> like this:
>
> {let count=0}
> {section loop code here}
> $count
> {let count=$count+1}
> {/section}
>

There is some limit statement in loop, maybe it can help you; also loop can work on arrays (look for exact syntax in example template full_class_29.tpl that EZ provides but generally it will look like:

{section name="i" loop=10 sequence=array(1,2,3,4,5) }
<p>$i:sequence}</p>
{/section}

will print out

1
2
3
4
5
1
2
3
4
5

Mike Cohen

Friday 17 January 2003 8:33:03 am

> > Is there some way to use an if-else type statement in
> the
> > template files?
> >
> > For example to only show a Heading if there is actually
> > content under the heading. I've tried setting:
> >
> > {let
> >
> list_count=fetch('content','list_count',hash(parent_node_id,$node.node_id))}
> >
> > Then wrapping the conditional text in a {section} block
> with
> > the following types of {section} code, but I really
> don't
> > know what they mean or even if I'm on the right track.
> > {section show=$list_count}
> > {section show=eq($list_count)}
> > {section show=and($list_count)}
> >
>
> You are right, you may this syntax:
>
> {section show=gt($list_count,0)}
> ...if ther is some content ...
> {section-else}
> ...if there is no content...
> {/section}
>
> obviously, operators and, not, or, gt, eq are normal logical
> operators, just taking operands inside brackets.
>
> In case you doubt in what meaning your variable takes print
> debug information for yourself, just to know to what value
> to compare:
>
> {$list_count} will print its value on output HTML page.
>
> > Also wanted to know if there is a way to set and display a
>
> > count variable within a loop.
> >
> > like this:
> >
> > {let count=0}
> > {section loop code here}
> > $count
> > {let count=$count+1}
> > {/section}
> >
>
> There is some limit statement in loop, maybe it can help
> you; also loop can work on arrays (look for exact syntax in
> example template full_class_29.tpl that EZ provides but
> generally it will look like:
>
> {section name="i" loop=10 sequence=array(1,2,3,4,5) }
> <p>$i:sequence}</p>
> {/section}
>
> will print out
>
> 1
> 2
> 3
> 4
> 5
> 1
> 2
> 3
> 4
> 5

Thanks for the reply, I got the if-else code working, haven't tried the counting yet. One note on this code:

> {section show=gt($list_count,0)}
> ...if ther is some content ...
> {section-else}
> ...if there is no content...
> {/section}

I found that it didn't work because it appears if the list count is 0, null is actually returned, so the operator can't compare null with 0 and it just displays the content regarless. I ended up just using:

{section show=$list_count}
...if ther is some content ...
{section-else}
...if there is no content...
{/section}

and it worked fine.

Thanks for the help,

Mike

Mike Cohen

Friday 17 January 2003 9:19:17 am

> > Is there some way to use an if-else type statement in
> the
> > template files?
> >
> > For example to only show a Heading if there is actually
> > content under the heading. I've tried setting:
> >
> > {let
> >
> list_count=fetch('content','list_count',hash(parent_node_id,$node.node_id))}
> >
> > Then wrapping the conditional text in a {section} block
> with
> > the following types of {section} code, but I really
> don't
> > know what they mean or even if I'm on the right track.
> > {section show=$list_count}
> > {section show=eq($list_count)}
> > {section show=and($list_count)}
> >
>
> You are right, you may this syntax:
>
> {section show=gt($list_count,0)}
> ...if ther is some content ...
> {section-else}
> ...if there is no content...
> {/section}
>
> obviously, operators and, not, or, gt, eq are normal logical
> operators, just taking operands inside brackets.
>
> In case you doubt in what meaning your variable takes print
> debug information for yourself, just to know to what value
> to compare:
>
> {$list_count} will print its value on output HTML page.
>
> > Also wanted to know if there is a way to set and display a
>
> > count variable within a loop.
> >
> > like this:
> >
> > {let count=0}
> > {section loop code here}
> > $count
> > {let count=$count+1}
> > {/section}
> >
>
> There is some limit statement in loop, maybe it can help
> you; also loop can work on arrays (look for exact syntax in
> example template full_class_29.tpl that EZ provides but
> generally it will look like:
>
> {section name="i" loop=10 sequence=array(1,2,3,4,5) }
> <p>$i:sequence}</p>
> {/section}
>
> will print out
>
> 1
> 2
> 3
> 4
> 5
> 1
> 2
> 3
> 4
> 5

I'm trying to also use similar logic to display the author of an article only if the show author checkbox has been ticked. I've attempted to use:

{section show=((attribute_view_gui attribute=$node.object.data_map.show_author),yes)}
<p>{attribute_view_gui attribute=$node.object.data_map.author}</p>
{/section}

But I know the syntax is incorrect. The statement: {attribute_view_gui attribute=$node.object.data_map.show_author} returns yes or no, but I can't figure out how to compare this in a {section}statement. Any ideas?

Adolfo Barragán

Saturday 18 January 2003 10:54:47 pm

> I'm trying to also use similar logic to display the author
> of an article only if the show author checkbox has been
> ticked. I've attempted to use:
>
> {section show=((attribute_view_gui
> attribute=$node.object.data_map.show_author),yes)}
> <p>{attribute_view_gui
> attribute=$node.object.data_map.author}</p>
> {/section}
>
> But I know the syntax is incorrect. The statement:
> {attribute_view_gui
> attribute=$node.object.data_map.show_author} returns yes or
> no, but I can't figure out how to compare this in a
> {section}statement. Any ideas?

I think you forgot "eq" operator:

{section show=eq( (attribute_view_gui attribute=$node.object.data_map.show_author), yes)}

Also, I don't know if "yes" must be quoted.

Mike Cohen

Wednesday 22 January 2003 10:31:37 am

&gt; &gt; I'm trying to also use similar logic to display the
&gt; author
&gt; &gt; of an article only if the show author checkbox has been
&gt; &gt; ticked. I've attempted to use:
&gt; &gt;
&gt; &gt; {section show=((attribute_view_gui
&gt; &gt; attribute=$node.object.data_map.show_author),yes)}
&gt; &gt; &lt;p&gt;{attribute_view_gui
&gt; &gt; attribute=$node.object.data_map.author}&lt;/p&gt;
&gt; &gt; {/section}
&gt; &gt;
&gt; &gt; But I know the syntax is incorrect. The statement:
&gt; &gt; {attribute_view_gui
&gt; &gt; attribute=$node.object.data_map.show_author} returns yes
&gt; or
&gt; &gt; no, but I can't figure out how to compare this in a
&gt; &gt; {section}statement. Any ideas?
&gt;
&gt; I think you forgot &quot;eq&quot; operator:
&gt;
&gt; {section show=eq( (attribute_view_gui
&gt; attribute=$node.object.data_map.show_author), yes)}
&gt;
&gt; Also, I don't know if &quot;yes&quot; must be quoted.

I've tried every variation and operator possible to no avail. I don't know if attribute_view_gui
attribute=$node.object.data_map.show_author can be compared in a statement like this, since it has an equals sign, and is normally displayed within {}. I've tried:

{section show=eq( {attribute_view_gui
attribute=$node.object.data_map.show_author}, yes)}

to no avail.

I have noticed one thing though, when:
{attribute_view_gui
attribute=$node.object.data_map.show_author} displays in the template, it doesn't just display the variable value, eZ Publish wraps it in &lt;p class=&quot;box&quot;&gt;&lt;/p&gt; tags. I have edited the file design\standard\templates\content\datatype\view\ezemail.tpl so that only the value displays but still no luck.

Mike Cohen

Wednesday 22 January 2003 10:58:25 am

> > I'm trying to also use similar logic to display the
> author
> > of an article only if the show author checkbox has been
> > ticked. I've attempted to use:
> >
> > {section show=((attribute_view_gui
> > attribute=$node.object.data_map.show_author),yes)}
> > <p>{attribute_view_gui
> > attribute=$node.object.data_map.author}</p>
> > {/section}
> >
> > But I know the syntax is incorrect. The statement:
> > {attribute_view_gui
> > attribute=$node.object.data_map.show_author} returns yes
> or
> > no, but I can't figure out how to compare this in a
> > {section}statement. Any ideas?
>
> I think you forgot "eq" operator:
>
> {section show=eq( (attribute_view_gui
> attribute=$node.object.data_map.show_author), yes)}
>
> Also, I don't know if "yes" must be quoted.

I've tried every variation and operator possible to no avail. I don't know if attribute_view_gui
attribute=$node.object.data_map.show_author can be compared in a statement like this, since it has an equals sign, and is normally displayed within {}. I've tried:

{section show=eq( {attribute_view_gui
attribute=$node.object.data_map.show_author}, yes)}

to no avail.

I have noticed one thing though, when:
{attribute_view_gui
attribute=$node.object.data_map.show_author} displays in the template, it doesn't just display the variable value, eZ Publish wraps it in <p class="box"></p> tags. I have edited the file design\standard\templates\content\datatype\view\ezemail.tpl so that only the value displays but still no luck.

Bruce Morrison

Wednesday 22 January 2003 2:35:57 pm

> > > I'm trying to also use similar logic to display
> the
> > author
> > > of an article only if the show author checkbox has
> been
> > > ticked. I've attempted to use:
> > >
> > > {section show=((attribute_view_gui
> > >
> attribute=$node.object.data_map.show_author),yes)}
> > > <p>{attribute_view_gui
> > >
> attribute=$node.object.data_map.author}</p>
> > > {/section}
> > >
> > > But I know the syntax is incorrect. The
> statement:
> > > {attribute_view_gui
> > > attribute=$node.object.data_map.show_author}
> returns yes
> > or
> > > no, but I can't figure out how to compare this in
> a
> > > {section}statement. Any ideas?
> >
> > I think you forgot "eq" operator:
> >
> > {section show=eq( (attribute_view_gui
> > attribute=$node.object.data_map.show_author), yes)}
> >
> > Also, I don't know if "yes" must be quoted.
>
> I've tried every variation and operator possible to no
> avail. I don't know if attribute_view_gui
> attribute=$node.object.data_map.show_author can be compared
> in a statement like this, since it has an equals sign, and
> is normally displayed within {}. I've tried:
>
> {section show=eq( {attribute_view_gui
> attribute=$node.object.data_map.show_author}, yes)}
>
> to no avail.
>
> I have noticed one thing though, when:
> {attribute_view_gui
> attribute=$node.object.data_map.show_author} displays in the
> template, it doesn't just display the variable value, eZ
> Publish wraps it in <p
> class="box"></p> tags. I have edited the
> file
> design\standard\templates\content\datatype\view\ezemail.tpl
> so that only the value displays but still no luck.

Hi Mike

The {attribute_view_gui ... } seems to be designed to display the attribute of the object not return the actual value of it.

I suspect that there may be another template function to allow this access (It may not exist as yet). The rest of your template code looks find we just need to find the right function :)

Bruce

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

sergey podlesnyi

Thursday 23 January 2003 4:13:35 am

>
> {section show=eq( {attribute_view_gui
> attribute=$node.object.data_map.show_author}, yes)}
>

Try this:

{section show=eq($node.object.data_map.show_author.content, "1")}

Mike Cohen

Thursday 23 January 2003 8:26:45 am

> >
> > {section show=eq( {attribute_view_gui
> > attribute=$node.object.data_map.show_author}, yes)}
> >
>
>
> Try this:
>
> {section show=eq($node.object.data_map.show_author.content,
> "1")}

Sergei,

Thank you very much, that worked.

Mike

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.