Scope of smarty variables

Get help with installation and running PivotX 2.0.x here. Please do not post Extension or Theme related questions here.

Scope of smarty variables

Postby ljhelbo » Thu May 17, 2012 2:57 pm

I have a problem with smarty variables, which I can not really figure out. In some themes we have sliders or others ways of showing and presenting selected entries. They must usually contain a certain (fixed) number of entries selected according to (a set of) different rules. E.g. I look through the weblog and pick entries complying to rule A. Then I look through the same weblog once more looking for entries complying to rule B etc. During this I count the number of found/picked out entries and when I have the total wanted number, I stop the procedure.

For this purpose I came up with this:

Code: Select all
  <ul class="slides">
      [[ assign var=count value=0 ]]
         [[ subweblog name="standard"]][[ literal ]]
            [[ if ($count<4) ]]
               [[ if $entry.extrafields.a ]]
                  [[ include file="`$templatedir`/_sub_gallery_entry.tpl" ]]
                  [[ assign var=count value=$count+1 ]]
               [[ /if ]]
            [[ /if ]]
         [[ /literal ]][[ /subweblog ]]
         [[ subweblog name="standard"]][[ literal ]]
            [[ if ($count<4) ]]
               [[ if ($entry.extrafields.b and !$entry.extrafields.a) ]]
                  [[ include file="`$templatedir`/_sub_gallery_entry.tpl" ]]
                  [[ assign var=count value=$count+1 ]]
               [[ /if ]]
            [[ /if ]]
         [[ /literal ]][[ /subweblog ]]
         [[ subweblog name="standard"]][[ literal ]]
            [[ if ($count<4) ]]
               [[ if (!$entry.extrafields.a and !$entry.extrafields.b) ]]
                  [[ include file="`$templatedir`/_sub_gallery_entry.tpl" ]]
                  [[ assign var=count value=$count+1 ]]
               [[ /if ]]
            [[ /if ]]
         [[ /literal ]][[ /subweblog ]]
  </ul>


Here i have simply defined two extrafields a and b, which may be set or not. I want a total of 4 entries in the slider. I would prefer entries, where a is set. If there are not enough of these, I will take some with b and if I still have too few, I will take some where neither a nor b is set. BUT under all circumstances I want a maximum of 4 entries. Therefore I define a variable $count. Each time, when I find a suitable entry this counter is increased.

This is only an example of course. It could also be that I would prefer entries with extra.imagefield filled or anything else.

But my problem is that each time I start a new run through the weblog, the counter is reset to 0. So what I get is up to 4 entries of type a followed by up to 4 entries of type b etc. This looks like something with the scope of this variable. As if its scope is only within the subweblog-tag. I tried to play with the scope-parameter of assign (global, root or parent), but that does not change anything. What am i doing wrong?
ljhelbo
 
Posts: 104
Joined: Sat Feb 26, 2011 2:58 pm
Location: Denmark

Re: Scope of smarty variables

Postby ljhelbo » Thu May 17, 2012 6:53 pm

OK, now i found a "solution" - it works, but I think it ought to be a little more simple than that?

Code: Select all
 <ul class="slides">
   [[ assign var=count value=0 ]]
      [[ subweblog name="standard"]][[ literal ]]
         [[ if ($count<4) ]]
            [[ if $entry.extrafields.a ]]
               [[ include file="`$templatedir`/_sub_gallery_entry.tpl" ]]
               [[ assign var=count value=$count+1 ]]
            [[ /if ]]
         [[ /if ]]
         [[ capture name="number" ]][[ $count ]][[ /capture ]]
      [[ /literal ]][[ /subweblog ]]
      [[ assign var=count value=$smarty.capture.number ]]
      [[ subweblog name="standard"]][[ literal ]]
         [[ if ($count<4) ]]
            [[ if ($entry.extrafields.b and !$entry.extrafields.a) ]]
               [[ include file="`$templatedir`/_sub_gallery_entry.tpl" ]]
               [[ assign var=count value=$count+1 ]]
            [[ /if ]]
         [[ /if ]]
         [[ capture name="number" ]][[ $count ]][[ /capture ]]
      [[ /literal ]][[ /subweblog ]]
      [[ assign var=count value=$smarty.capture.number ]]
      [[ subweblog name="standard"]][[ literal ]]
         [[ if ($count<4) ]]
            [[ if (!$entry.extrafields.a and !$entry.extrafields.b) ]]
               [[ include file="`$templatedir`/_sub_gallery_entry.tpl" ]]
               [[ assign var=count value=$count+1 ]]
            [[ /if ]]
         [[ /if ]]
      [[ /literal ]][[ /subweblog ]]
 </ul>
ljhelbo
 
Posts: 104
Joined: Sat Feb 26, 2011 2:58 pm
Location: Denmark

Re: Scope of smarty variables

Postby Schop » Thu May 17, 2012 8:40 pm

I have run into the exact same problem recently. The problem is caused by the [[literal]] tag, I think, and it is just a quirk of the Smarty engine that we'll have to deal with.

If your solution works, I would leave it like that, or write some fancy extension that does all the work for you.
User avatar
Schop
Contributor
 
Posts: 484
Joined: Mon Apr 21, 2008 1:47 pm
Location: Hudson, Ohio

Re: Scope of smarty variables

Postby hansfn » Fri May 18, 2012 10:48 am

I did a quick Google search and the capture approach seems to be common among Smarty users/developers.

I wonder if the following works? It avoids jumping back and forth - using the capture variable all the time.

Code: Select all
<ul class="slides">
      [[ capture name="count" ]]0[[ /capture ]]
      [[ subweblog name="standard"]][[ literal ]]
         [[ if ($smarty.capture.count<4) ]]
            [[ if $entry.extrafields.a ]]
               [[ include file="`$templatedir`/_sub_gallery_entry.tpl" ]]
               [[ capture name="count" ]] [[ $smarty.capture.count+1 ]] [[ /capture ]]
            [[ /if ]]
         [[ /if ]]
      [[ /literal ]][[ /subweblog ]]
...
hansfn
Developer
 
Posts: 3230
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Scope of smarty variables

Postby ljhelbo » Fri May 18, 2012 12:31 pm

Yes, it does, thanks :D
ljhelbo
 
Posts: 104
Joined: Sat Feb 26, 2011 2:58 pm
Location: Denmark


Return to 2.x Support Forum

Who is online

Users browsing this forum: No registered users and 2 guests

cron