[Solar-talk] Template Parser / Compiler
Paul M Jones
pmjones at ciaweb.net
Mon May 28 14:28:32 PDT 2007
On May 1, 2007, at 4:06 AM, Rodrigo Moraes wrote:
> On 7/28/06, Paul M Jones wrote:
>> On Jul 19, 2006, at 9:54 AM, Rodrigo Moraes wrote:
>>> The simple question is: will Solar_View have a parser / compiler
>>> plugin one day?
>>
>> I don't see why not. My bet is that it would make more use of
>> streams than Savant3 does.
>>
>> However, I also think this might be a good candidate for a helper
>> class to take user markup and translate it to output. Then only the
>> user-provided portion of the view has to be run through the helper,
>> rather than run an entire template series through a separate
>> compiler.
>
> This was asked a long time ago... before Solar_Markdown and a lot of
> other things.
>
> One solution I came up with yesterday was to combine a view helper
> with a Solar_Markdown plugin. Basically a view helper calls
> Solar_Markdown to parse a template which may contain special tags
> like:
>
> <plugin name="calendar" />
>
> A special Solar_Markdown_Plugin then parses all <plugin> tags and
> returns back the result of a view helper defined in the "name"
> attribute. In the example above, it will replace the tag by the result
> of the view helper *_Helper_Calendar.
>
> Other attributes can be passed to the view helper, so:
>
> <plugin name="blogRoll" amount="5" />
>
> ...will return the result of the helper *_Helper_BlogRoll passing
> array('amount' => 5) as parameter.
>
> It's a very simple solution, and now I'm thinking if I really need
> Solar_Markdown for this, because I could just parse the special tag
> inside a view helper with the same technique used by Solar_Markdown
> plugins. Thoughts?
That's a pretty neat idea. :-) A few points:
1. Be wary of security issues here, and make sure the "plugin" parser
won't let you execute just any helper. Users are very tricky.
2. Markdown parsing is relative resource-intensive, and I like to
cache to the converted HTML when I can. Parsing from inside the view
might make a caching strategy more difficult (but certainly not
impossible).
3. I would say instead of a helper, it might be better to have a
series of plugin parsers, one for each content module you want to
have available. So instead of `<plugin name="blogRoll" key="val">`,
have a Tipos_Markdown_Plugin_BlogRoll plugin that gets called with `
{blogroll key="val"}`. This errs on the side of security and make it
easier to parse (and then cache) from within the controller.
Hope this helps.
--
Paul M. Jones <http://paul-m-jones.com>
Solar: Simple Object Library and Application Repository
for PHP5. <http://solarphp.com>
Join the Solar community wiki! <http://solarphp.org>
Savant: The simple, elegant, and powerful solution for
templates in PHP. <http://phpsavant.com>
More information about the solar-talk
mailing list