Announcement

  •  » Extensions
  •  » register_modifier in new theme?

#1 2013-11-10 17:42:19

marjolein
Member
Amsterdam, NL
2013-11-10
29

register_modifier in new theme?

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:

Code:

// 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

 

#2 2013-11-10 17:45:36

flop25
Piwigo Team
2006-07-06
7037

Re: register_modifier in new theme?

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


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 

#3 2013-11-10 17:50:03

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3277

Re: register_modifier in new theme?

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

 

#4 2013-11-10 17:59:34

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: register_modifier in new theme?

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

 

#5 2013-11-10 18:01:36

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: register_modifier in new theme?

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

 

#6 2013-11-10 18:13:56

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: register_modifier in new theme?

Hmm,

I changed the code in my themeconfig.inc.php to:

Code:

// 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

 

#7 2013-11-10 18:16:46

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3277

Re: register_modifier in new theme?

right you need to wait $template is initialized ;

Code:

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

 

#8 2013-11-10 18:16:47

flop25
Piwigo Team
2006-07-06
7037

Re: register_modifier in new theme?

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


To get a better help : Politeness like Hello-A link-Your past actions precisely described
Check my extensions : more than 30 available
who I am and what I do : http://fr.gravatar.com/flop25
My gallery : an illustration of how to integrate Piwigo in your website

Offline

 

#9 2013-11-10 18:51:30

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: register_modifier in new theme?

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:

Code:

// 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

 

#10 2013-11-10 19:07:17

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: register_modifier in new theme?

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

 

#11 2013-11-10 19:13:34

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3277

Re: register_modifier in new theme?

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

 

#12 2013-11-10 19:33:07

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: register_modifier in new theme?

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

 

#13 2013-11-10 19:38:21

mistic100
Former Piwigo Team
Lyon (FR)
2008-09-27
3277

Re: register_modifier in new theme?

your site :) (or localhost)

Offline

 

#14 2013-11-10 19:47:30

marjolein
Member
Amsterdam, NL
2013-11-10
29

Re: register_modifier in new theme?

Got it! Brilliant :D

Cheers,
Marjolein

Offline

 
  •  » Extensions
  •  » register_modifier in new theme?

Board footer

Powered by FluxBB

github twitter newsletter Donate Piwigo.org © 2002-2024 · Contact