Hi all,
What I'm starting from: I'm quite new to Piwigo (installed 2.5.3, started just a few weeks ago) but not new to PHP programming or Smarty templating (it's been a while, but I'm more used to Smarty 3 than 2.6...).
I've embarked on developing my own theme, loosely based on the default theme and dark (aiming for HTML5, CSS3, responsive design and configurability - not all at the same time :) ).Currently I'm working on the toolbars and 'popover' boxes.
For the Calendar view I'm running into a little problem: I want to adapt the width of some elements to the context it's running in (years, weeks or days) and reasoned I could deduce this by looking for the number of slashes in the 'breadcrumb' shown above; the string for the whole path is available in {$chronology.TITLE}. So far so good.
Parsing a string with Smarty is theoretically possible, but tedious, and should really be done with PHP - so I thought I could create a little function to 'count slashes' and registering this as a modifier in Smarty. Now comes the problem: I don't know quite how Piwigo interacts with Smarty to accomplish this. I've seen that:
* it's possible to include 'extra code' in a theme's themeconfig.inc.php
* the Template class does a number of register_modifier calls in the constructor
I've tried adding the following to my themeconfig.inc.php:
// count slashes modifier function count_slashes( $string ) { return strlen( $string ) - strlen( str_replace( '/', '', $string ) ); } $smarty->register_modifier('count_slashes', 'count_slashes' );
But on the last line PHP throws a fatal error "Call to a member function register_modifier() on a non-object".
So, in that context clearly there is no $smarty object yet.
The question is: what do I need to do or how do I 'wrap' my code so that I can access the $smarty object?
(Or, alternatively, is there another way to solve this problem - without hacking Piwigo's core code but staying strictly within the theme?)
Last edited by marjolein (2013-11-10 17:49:58)
Offline
Hi
very interesting project here, don't forget to come back here to present the result :)
if you want to use php, you could use a prefilter and the global var $page to know what's the display mode, then assign a custom variable for your theme.
The skeleton Plugin should help you
Offline
there is no $smarty variable in Piwigo, it is wrapped in our own templating class
try
global $template;
$template->smarty->register_modifier('count_slashes', 'count_slashes' );
Offline
Hi Flop,
That was fast :)
I'm definitely planning on sharing my theme once it is at least basically functional, but it isn't yet. I have a site where I experiment, but don't want to give out the address too much yet - too many scaffolds and lines for now!
I just downloaded the Skeleton plugin and will study it! From what you say it sounds like that approach might work. Will report back....
Cheers,
Marjolein
Offline
Hi mistic100,
Great, another approach! I love having different options to tackle a problem.I'll try that (as well as Flop25's suggestion) and see where that takes me. Thanks!
Cheers,
Marjolein
Offline
Hmm,
I changed the code in my themeconfig.inc.php to:
// count slashes modifier function count_slashes( $string ) { return strlen( $string ) - strlen( str_replace( '/', '', $string ) ); } global $template; $template->smarty->register_modifier('count_slashes', 'count_slashes' );
Unfortunately, now I don't just get an error, but a notice as well:
* Notice: Trying to get property of non-object
* Fatal error: Call to a member function register_modifier() on a non-object
both on the last line.
So much for that - I'm guessing the code is correct but not quite in the correct context: there's obviously not $template object here (yet) either.
I'll try the prefilter approach then - that will take me a little more time...
Cheers,
Marjolein
Offline
right you need to wait $template is initialized ;
function count_slashes( $string ) { return strlen( $string ) - strlen( str_replace( '/', '', $string ) ); } add_event_handler('init', 'yout_init'); function yout_init() { global $template; $template->smarty->register_modifier('count_slashes', 'count_slashes' ); }
not sure though
Offline
not exactly a prefilter in fact, sry
use a trigger, global $page and i'm sure it will contain enougth info to assign then a custom smarty variable for your template
Offline
Wonderful, again two different approaches...
And before I saw those just now I had a little brainwave too (inspired by the code I saw in the Pure_default theme's themeconfig.inc.php), and figured out yet another solution:
// count slashes modifier function count_slashes( $string ) { $count = 0; $str = str_replace( '/', '', strip_tags($string), $count ); # get rid of link tags first! return $count; } add_event_handler('loc_begin_page_header', 'regmod'); function regmod() { global $template; $template->smarty->register_modifier('count_slashes', 'count_slashes' ); }
This works correctly, without any grumbles from PHP.
(I was confused at first when my first working version told me there were 13 slashes... until I realized that 'string' actually contained a couple of links, too. Duh.)
I'll study both your approaches a bit more, but I'm glad I have this little modifier working (even if it may not be the 'best' solution) so I can write some conditional code in my template to assign a class to style with. :D
Cheers,
Marjolein
Last edited by marjolein (2013-11-10 18:54:25)
Offline
I see my solution is almost identical to mistic100's solution.
I haven't seen where it lives, but my guess is 'init' is triggered earlier than 'loc_begin_page_header'. Is this correct?
And, for something like this (registering a modifier) is it best to do it as early as possible or as late as possible? Any insights?
Cheers,
Marjolein
Offline
I don't think it changes anything (for your use case, but some templates files are parsed before your trigger)
tip: go to http:///..../tools/triggers_list.php for a full list of all triggers
Offline
mistic100,
OK, if some template related things have already happened by the time 'loc_begin_page_header' comes by, 'init' sounds like a cleaner approach, so I've adapted my code to use that.
As to http:///..../tools/triggers_list.php -- what do I fill in on the dots? I tried 'piwigo.org' but got a 'broken link' page. A list of all triggers sounds quite useful!
Cheers,
Marjolein
Offline
your site :) (or localhost)
Offline
Got it! Brilliant :D
Cheers,
Marjolein
Offline