Access template_dir from within attributes

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

Access template_dir from within attributes

Postby xzjia » Thu Nov 26, 2009 8:33 pm

Is there someway to access [[ template_dir ]] from within another pivotx tag?

Such as [[ pagelist ]]

I'm looking to include icons as part of the list, and I need to specify the image directory and having a dynamic reference saves a lot of testing effort (I'm testing with a different installation).

Code: Select all
pages="<li %active%><a href='%link%' title='%title%'><span><img src='/**** need reference here ****/img/icons/small/%subtitle%.png' />%title%</span></a></li>"


As you can see I used subtitle as the dynamic name for the title icon, since I have no use for subtitle for pages anyways; but of course you can't put [[ template_dir ]] inside of [[ pagelist ]], hence my problem.

The obvious solution is making it a CSS background instead, however that only means the CSS will be hardcoded with the urls, and if I do inline CSS I'm back to where I started.
xzjia
 
Posts: 110
Joined: Tue Dec 11, 2007 6:26 pm

Re: Access template_dir from within attributes

Postby hansfn » Fri Nov 27, 2009 12:12 am

Why exactly can't you set the CSS class (for the span) to be "something-%subtitle%" and then in your stylesheet set the img URLs for the background. Or did I misunderstand?
hansfn
Developer
 
Posts: 3282
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Access template_dir from within attributes

Postby xzjia » Fri Nov 27, 2009 1:27 am

Hmmmm, so you mean creating a bunch of classes with the names of images, and set the url for the images there, and then referencing those classes?

Except I'm using an icon pack with like 30 images, if I could help it I rather not place 30x classes in the CSS....

Actually I found something that works very well (except isactive): [[ getpagelist ]]

Code: Select all
[[ getpagelist var='pagelist' onlychapter='pages' exclude='links' ]]

[[ foreach from=$pagelist item='pageuri' ]]
 [[ getpage uri=$pageuri ]]
<li><a href='[[ $page.link ]]' title="[[ $page.title ]]"><span><img src='[[ template_dir ]]/img/icons/small/[[ $page.subtitle ]].png' />[[ $page.title ]]</a></span></li>
 [[ resetpage ]]
[[ /foreach ]]


Everything works, except that I need to somehow get the functionality of %active% back in there, I tried to go the $pageuri comparison route, except that the $pageuri get set to the value of the current page item. So I guess I need to somehow copy the $pageuri value, store it, compare it with each of the page items, and stick a id="active" in there if they equate.
xzjia
 
Posts: 110
Joined: Tue Dec 11, 2007 6:26 pm

Re: Access template_dir from within attributes

Postby hansfn » Fri Nov 27, 2009 7:48 am

To get the URI of the page you can use [[$modifier.uri]].

PS! You never told us that you needed to handle 30 different images. How do you manage to remember them when setting the subtitle ;-)

PS! If you are only testing, another option is to write some Javascript that set/modifies those image URLs. (It wouldn't be many lines.)
hansfn
Developer
 
Posts: 3282
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Access template_dir from within attributes

Postby xzjia » Fri Nov 27, 2009 4:18 pm

Thanks for that variable.

Here's the complete solution for smarty tags INSIDE menu logic:

Code: Select all
<div id="topnav">
<ul>
   [[ getpagelist var='subpagelist' onlychapter=$page.title exclude='links' ]]
   
   [[ foreach from=$subpagelist item='pageuri' ]]
   [[ getpage uri=$pageuri ]]
   [[ if $page.status eq 'publish' ]] [[* Detect Publish Status *]]
   <li [[ if $modifier.uri eq $pageuri]] class='active' [[/if]]><a href='[[ $page.link ]]' title="[[ $page.title ]]">[[ $page.title ]]</a></li>
   [[ /if]]
   [[ resetpage ]]
   [[ /foreach ]]
</ul>
</div>


Btw. does [[ $modifier ]] work with other variables such as $title, $chaptername etc...? I'm interested to do some complex menu logic for a custom 2-tier menu layout.
xzjia
 
Posts: 110
Joined: Tue Dec 11, 2007 6:26 pm

Re: Access template_dir from within attributes

Postby xzjia » Fri Nov 27, 2009 9:14 pm

Here's the logic I'm using for the submenu, I'm putting it right before the code previously posted.

Code: Select all
   [[ if $chaptername eq 'Pages' ]] [[* Detect Page Hierarchy, Select Correct Chapter *]]
      [[ getpagelist var='subpagelist' onlychapter=$page.title exclude='links' ]]
   [[ elseif $chaptername neq 'Pages' ]]
      [[ getpagelist var='subpagelist' onlychapter=$chaptername exclude='links' ]]
   [[ /if ]]


The reason I can't use the included extension is because I need the menu to be 2-part, each with its own active state detection. Right now stuck at the topnav, because I need to compare $chaptername of the current page ($modifier.chaptername?) with $page.title for active state.
xzjia
 
Posts: 110
Joined: Tue Dec 11, 2007 6:26 pm

Re: Access template_dir from within attributes

Postby hansfn » Sat Nov 28, 2009 2:23 am

The Smarty variable $modifier only contains (some) information about the request - nothing about the page or entry itself.
hansfn
Developer
 
Posts: 3282
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Access template_dir from within attributes

Postby xzjia » Sat Nov 28, 2009 4:34 am

I was hoping I can find some way to implement the menu logic with smarty tags at the template level, but it seems that diving into the deep end and creating / modifying custom extension might be required.

Right now all I need is somehow sneak the $chaptername string into the getpagelist without being over-ridden for comparison purpose. If there's a way to store / recall variable at the template level that'll scratch the itch.
xzjia
 
Posts: 110
Joined: Tue Dec 11, 2007 6:26 pm

Re: Access template_dir from within attributes

Postby hansfn » Sat Nov 28, 2009 2:10 pm

Code: Select all
[[ assign var='foo' value=$somevar ]]

enables you to use $foo later on, preserving the value of $somevar. Isn't this what you want?
hansfn
Developer
 
Posts: 3282
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Access template_dir from within attributes

Postby xzjia » Sat Nov 28, 2009 3:41 pm

Muhahahaha! Beautiful! Full template logic goodness, no need for extensions (though extending the core by storing more info into $modifier would prove infinitely useful).

Here's the code for selecting active state for the main menu (looking for either $chaptername/$page.title match or URI match)

Code: Select all
      <div id="topnav">
      <ul>
         [[ assign var='src_chaptername' value=$page.chaptername ]]

         [[ getpagelist var='pagelist' onlychapter='Pages' exclude='links' ]]
         
         [[ foreach from=$pagelist item='pageuri' ]]
         [[ getpage uri=$pageuri ]]
         [[ if $page.status eq 'publish' ]] [[* Detect Publish Status *]]
         <li [[ if ($modifier.uri eq $pageuri) || ($src_chaptername eq $page.title) ]] class='active' [[/if]]><a href='[[ $page.link ]]' title="[[ $page.title ]]"><span><img src='[[ template_dir ]]/img/icons/small/[[ $page.subtitle ]].png' />[[ $page.title ]]</a></span></li>
         [[ /if ]]
         [[ resetpage ]]
         [[ /foreach ]]
      </ul>
      </div>


Here's the modified hierarchy detection code, using [[ $chapterdesc ]], looking for "subchapter" in the chapter description so auxiliary pages don't get a list of other auxiliary pages.

Code: Select all
      [[ if $chaptername eq 'Pages' ]]
      [[ getpagelist var='subpagelist' onlychapter=$page.title exclude='links' ]]
      [[ elseif $page.chapterdesc eq 'subchapter' ]]
      [[ getpagelist var='subpagelist' onlychapter=$chaptername exclude='links' ]]
      [[ /if ]]


Finally, I'm using 960 Grids CSS framework, so I'm hiding the sidebar and changing the size of the content area depending on whether the page contains children at all:

Code: Select all
      [[ if count($subpagelist) ]]#sidebar#[[ /if ]]

Code: Select all
      <div id="content" class="grid_[[ if count($subpagelist) ]]8[[ else ]]12[[ /if ]]">



Everything's perfect now, thanks for all the help.
xzjia
 
Posts: 110
Joined: Tue Dec 11, 2007 6:26 pm


Return to 2.x Support Forum

Who is online

Users browsing this forum: No registered users and 6 guests

cron