Jump to content

PWUtils - Idea for micro library of template functions


saml
 Share

Recommended Posts

For a recent (low-budget)

... would instead become:

echo ul( li( 'Home' ) . li_foreach_page( $page->children ) );

I have collected the code I'm regularly re-using so far in a github repo: https://github.com/samuell/pwutils'>github.com/samuell/pwutils

I'm sure many of you are doing things like this already, but I was just thinking that this kind of thing could be a good idea help each other improve upon.

I was thinking that having a common such micro-library with all the common cases covered to speed things up, should be a productivity booster that we all can benefit from.

What do you guys think? Maybe there is something like this already, that I just missed? :)

  • Like 3
Link to comment
Share on other sites

I like the idea and was thinking also about it now and then. But never could get my head around it. Either too inflexible, and adding another layer with something that may prove as "not so useful" or will at some point grow and change, that it may not be consistent over the time. 

I like your approach to have a simple base that you may adapt to your project.

Here's what Ryan build some years ago when he built the blog profle. Took me a while to find my copy locally. I put it on github for other to see. At that time I was thinking to use it, or adapt it at some point but never picked it up..

Nevertheless interesting approach.

https://github.com/somatonic/BaseMarkupExample

  • Like 1
Link to comment
Share on other sites

I like the idea and was thinking also about it now and then. But never could get my head around it. Either too inflexible, and adding another layer with something that may prove as "not so useful" or will at some point grow and change, that it may not be consistent over the time. 

I like your approach to have a simple base that you may adapt to your project.

Here's what Ryan build some years ago when he built the blog profle. Took me a while to find my copy locally. I put it on github for other to see. At that time I was thinking to use it, or adapt it at some point but never picked it up..

Nevertheless interesting approach.

https://github.com/somatonic/BaseMarkupExample

Many thanks for the link, that is indeed useful code to study! Will have a look and see what ideas I can incorporate in my own collection too  :)

Link to comment
Share on other sites

I like this. I think a nice object oriented implementation in the background combined with pure php functions for the template context would be great :)

I would go as short as possible:

<div class="foo"><?= ul( li($pages->find('template=foo,limit=25') ) ) ?></div>
<?= ul( li('Home') . li($pages->get('/') ) ?>
<?= a($page) ?> // Produces a standard link to a page with href, 
<?= ul( li( a( img($page->images) ) ) ) ?>

^_^

  • Like 1
Link to comment
Share on other sites

I like this. I think a nice object oriented implementation in the background combined with pure php functions for the template context would be great :)

I would go as short as possible:

<div class="foo"><?= ul( li($pages->find('template=foo,limit=25') ) ) ?></div>
<?= ul( li('Home') . li($pages->get('/') ) ?>
<?= a($page) ?> // Produces a standard link to a page with href, 
<?= ul( li( a( img($page->images) ) ) ) ?>

^_^

Thanks! Indeed, with a little bit of type checking one should be able to do like this as well! ... and this is going towards where I was hoping to get in the end as well. I should probably give it another try and try to make it shorter, like you propose! :)

Link to comment
Share on other sites

Thanks! Indeed, with a little bit of type checking one should be able to do like this as well! ... and this is going towards where I was hoping to get in the end as well. I should probably give it another try and try to make it shorter, like you propose! :)

I like this. I think a nice object oriented implementation in the background combined with pure php functions for the template context would be great :)

I would go as short as possible:

<div class="foo"><?= ul( li($pages->find('template=foo,limit=25') ) ) ?></div>
<?= ul( li('Home') . li($pages->get('/') ) ?>
<?= a($page) ?> // Produces a standard link to a page with href, 
<?= ul( li( a( img($page->images) ) ) ) ?>

^_^

Well, thinking about this now, there is the problem that it is not exactly clear as to what to link, when embedding the functions in each other like this. 

E.g. one might want either to link directly to the image url, or to the image page, or something else, for each image.

It seems to me that the most general way to handle this, is to create a specialized map function for processwire PageArrays, which can be used with an anonymous function, like this:

echo ul( pagearray_map( function ( $p ) { return li( a( $p->url, $p->title ) ); }, $page->children ));

... this might be a little clearer if storing the anonymous function in a named variable, like so:

$func = function ( $p ) { return li( a( $p->url, $p->title ) ); };

echo ul( pagearray_map( $func, $page->children ));

But well ... things are getting slightly more involved now of course, though I think it is still manageable. Gotta chew on that  :)

Link to comment
Share on other sites

saml,

Nice work! I could see using this for some of the more simplistic/repetitive tasks. That said, it could perhaps be a rabbit hole that goes to deep if you try to account for all the scenarios that might be thrown at it. :)

Also: can we hide this from the noobs? :P

  • Like 2
Link to comment
Share on other sites

This reminds me of a "helper" from the diem cms/cmf that was pretty interesting back in the day. The code is still available as a standalone module on github but no longer maintained.

Another similar but different tool is "haml" from ruby, which there are some php ports of.

It would be interesting to try these out in the context of pw.

Thanks, very nice links! I liked especially the first one, with the css syntax support - Always good to avoid re-inventing the wheel in terms of syntax :)

Would be interesting to see how much of a performance hit there is to the regex parsing involved ... though hopefully that's not anything major. 

Personally I would probably have "shortcut" functions on the form of a( ), li(  ) anyway, which would pass on to any underlying library api.

Will keep these links in mind as I continue playing with this.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...