[PHPTAL] Dynamic macros

2008-10-21 Thread Maciej Łebkowski
Hello,

Sometimes its comfortable to generate macros from dynamic data.
Is there any way to use macros from memory instead of files?

For now, it seems that this code works:

   tal:block metal:use-macro=$dynamic/test /

$dynamic is a data-uri, i.e. data://text/plain;base64, . base64_encode(macro)

Together with new PHPTAL_DataSource, attached in PHPTAL_FileSourceResolver:

+$parsed = parse_url($path);
+if (data == $parsed['scheme']) {
+return new PHPTAL_DataSource($path);
+}

(PHPTAL_DataSource implementation isn`t interesting at all)


I dont know if there are any drawbacks — cache, optimisation, etc?

Or maybe I am missing some fundamential concepts?

Any comments?

-- 
Maciej Łebkowski, http://lebkowski.info/kontakt.php
___
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal


Re: [PHPTAL] Dynamic macros

2008-10-21 Thread Kornel Lesiński

On 21-10-2008 at 07:03:50 Maciej Łebkowski [EMAIL PROTECTED] wrote:


I dont know if there are any drawbacks — cache, optimisation, etc?


If you've implemented PHPTAL_Source well then cache should work fine as  
long as code of the macro doesn't change too often.



Or maybe I am missing some fundamential concepts?


It depends what are you trying to achieve.

If you want LISPy meta-syntax, you should implement a template prefilter.

If you're just trying to implement eval() in TAL, then don't do it - it's  
just asking for trouble (bugs in macro generation code could allow code  
injection).


Maybe you could just write one flexible macro that works with more than  
one specific dataset?
If you need to generate complex markup you could write expression modifier  
or call PHP function from template.


--
regards, Kornel

___
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal


Re: [PHPTAL] Dynamic macros

2008-10-21 Thread Kornel Lesiński

On 21-10-2008 at 12:52:02 Maciej Łebkowski [EMAIL PROTECTED] wrote:


I don`t think it is possible. ;-) It relies too much on the user input.

For example, the content of the page (markdown syntax) can be
extended with some data from atom feed.


Reading of atom feeds doesn't sound like responsibility of templates.


But I dont know WHERE
to insert this data. The user could write something like:


User? Don't forget that PHPTAL allows use of PHP code, so you shouldn't  
let users write TAL templates unless you trust them completely.


tal:block tal:define=byebye php:exec('rm -rf /')


   This is paragraph

   tal:block metal:use-macro=atom/contents tal:define=entry  
feeds/main/2 /
 -- this will be replaced with contents of main atom feed, second  
entry.



And all of the above is *content*, insertet to the template using:

   tal:block tal:content=structure markdown: content /


If you've got markdown parser that supports reading of atom feeds (?),  
then just implement that entirely in markdown parser.



(i don`t know if they can be chained, but thats not a problem)
And in the modifier I have access to my data via $ctx object, right?


Sort-of. You shouldn't use $ctx directly. Use phptal_tale() instead (and  
if you do, your expression will be chainable).


--
regards, Kornel

___
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal