[PHPTAL] variable variables in phptal?
Is it possible to use variable variables in PHPTal ? eg I have a PHP array $phptal-fieldnames = array(array('key'='first_name', 'description'='First Name'), array('key'='address', 'description='Address'); and an object $person= new stdClass(); $person-first_name = 'Joe'; $person-address='His_street 1'; $phptal-person = $person; and in my template I'd like to use the key of fieldnames in a repeat ... div tal:repeat=field fieldnames ${field/description}: ${person/${field/key}} /div This doesn't work - but is there an other way to achieve it ? ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] nested UL
I have a multilevel array, where, as the template concerns, the number of levels is unlimited. eg $objects = array('Food'=array('Fruit'=array('Red'=array('Cherry', 'Strawberry'), 'Yellow'=array('Banana')), 'Meat'=array('Steak', 'Hamburger')); I'd like to loop through this array in my templates and end up with ul liFood ulliFruit ulliRed ulliCherre/liliStrawberry/li/ul /liliYellow ... etc How could I do this, without needing to know the number of levels? -- GRolf ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re: [PHPTAL] runtime generated slot names
I'm always interested in learning, so could you please enlighten me... why would you want to have changing names for a fixed slot? It's like having a certain function that can have different names. Most of the time, it works the other way around (a fixed name with a different functionality). GRolf Monday, November 8, 2010, 12:53:55 PM, you wrote: Hi all, I'd like to create a macro with fillable slots but I'd like the names of these slots to be based on variable information passed to the macro. It appears at the moment that the content of a 'define-slot=' is not processed like other content and is simply treated as a string. I'd prefer not to resort to macros because all the slots themselves are fully static - I just want to control which of these static slots are inserted at any time. I recognise that one possible suggested solution here might be a collection of conditionals to pick the slot but the macro I'm writing is in a library and the slot names are, therefore, not definable in advance. I hope this makes sense! In essence, my fill-slot statements are statically named but my define-slot statements need to be able to take variable string content. Anyone have any ideas? Robert ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] implode()
I often find myself using tal:repeat to implode an array in php $myarr = array('one', 'two', 'three'); is easily converted to one, two, three with implode(', ',$myarr); But in PHPTal, this is quite cumbersome span tal:repeat=val myarr ${val}span tal:condition=not:repeat/val/end, /span /span Isn't there an easier solution? ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re: [PHPTAL] callback?
and how can I solve this if I want to use it in my tal:repeat? e.g. for a Client, who has (potentially) multiple invoices tr tal:repeat=client clients td${client/name}/td tdspan tal:repeat=invoice client/invoices${invoice}tal:block tal:condition=not:repeat/invoice/end,/tal:block/span/td /tr this works fine if I retrieve all invoices before, but can I do it without looping through all clients in PHP first, if I'd have a function getInvoices($client_id)... ? Friday, June 18, 2010, 3:56:23 PM, you wrote: On 18-06-2010 at 14:46:13 GRolf ger...@pictureparking.com wrote: let's say I have a query that will fetch employees from a table. every row has a field department_id. when looping through this array in my template, is there a way to call a callback function that will fetch the department's name from an other table? PHPTAL automatically calls methods in TAL expressions. If you use ORM it should be quite simple, e.g. if you have Employee object that has getDepartment() method that returns Department object: li tal:repeat=employee employees tal:content=employee/getDepartment/name/ Alternatively you can call PHP functions using php: prefix in expressions: li tal:repeat=employee_row employees_array tal:block tal:define=department php:get_department_row(employee_row['department_id']) tal:content=department/name/ -- regards, Kornel ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] callback?
let's say I have a query that will fetch employees from a table. every row has a field department_id. when looping through this array in my template, is there a way to call a callback function that will fetch the department's name from an other table? ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re[2]: [PHPTAL] recursive ul/li
Marco, thanks It took my some time to figure out why this didn't work, untill I realised that I needed an extra div metal:use-macro=list/ to call the defined macro :s Now, it works like a charm! Monday, May 3, 2010, 5:47:43 PM, you wrote: Use Macros! ul metal:define-macro=list tal:define=listitems listitem li tal:repeat=listitem listitems ul metal:use-macro=list/ /li /ul This will generate nested ul's assuming that every listitem is an array of 0 or more arrays Marco Pivetta - Ocramius Aethril http://twitter.com/Ocramius 2010/5/3 GRolf ger...@pictureparking.com Is there a way to print an ulli tree recursively with PHPTal, that is, without knowing the exact number of levels in my associative array Say I have $phptal-myarray = Array ( [bar] = [baz] = Array ( [bop] = Array ( [0] = file4.txt ) [0] = file2.txt [1] = file3.txt ) [0] = file1.txt ) and I'd like to produce: ul liBar/li liBaz ul liBop ul liFile4.txt/li /ul /li lifile2.txt/li lifile3.txt/li /ul lifile1.txt/li /ul ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re[2]: [PHPTAL] recursive ul/li
To make matters worse ... would there be a way to show something else on the last level ? I'd like to show a checkbox, but only for the items that don't have any child items. Use Macros! ul metal:define-macro=list tal:define=listitems listitem li tal:repeat=listitem listitems ul metal:use-macro=list/ /li /ul This will generate nested ul's assuming that every listitem is an array of 0 or more arrays Marco Pivetta - Ocramius Aethril http://twitter.com/Ocramius 2010/5/3 GRolf ger...@pictureparking.com Is there a way to print an ulli tree recursively with PHPTal, that is, without knowing the exact number of levels in my associative array Say I have $phptal-myarray = Array ( [bar] = [baz] = Array ( [bop] = Array ( [0] = file4.txt ) [0] = file2.txt [1] = file3.txt ) [0] = file1.txt ) and I'd like to produce: ul liBar/li liBaz ul liBop ul liFile4.txt/li /ul /li lifile2.txt/li lifile3.txt/li /ul lifile1.txt/li /ul ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] auto-select checkboxes
I have the feeling that this should be possible, and maybe trivial, but I don't know how to solve it. I have an object ($person), who lives in a state ($person-state). I also have a list of states ($states). Both are assigned to phptal. $phptal-person = $person; $phptal-states = $states; In my PHPTAl template I have select name=state option tal:repeat=state states value=${state}${state}/option /select I'd like PHPtal to put the state of $person to selected. I know how to achieve it when I loop through the states array in PHP first, add an active flag to the one state that is active and use tal:attributes then, but would it be possible to achieve it without looping through all states in php? something like ... option tal:repeat=state states tal:attributes=selected person/state eq state value=${state}${state}/option ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re[2]: [PHPTAL] ajax and phptal
Since I was struggling with this too, I'm very interested in your view. I use a different approach: since a PHP-driven website usually consists of many .php scripts, I like to keep my templates organised in a 1-template-for-each-php-script (eg. index.php uses index.tpl, subscribe.php uses subscribe.tpl). Otherwise you're more busy with opening and closing templates to make changes (and trying to remember which file you need) than making the changes. I have 1 file (general.tpl) that holds the general page structure of every page, and then the 1-template-per-script as described above. my general.tpl has something like (more complex than the example below of course) html metal:define-macro=globalpage body div id=header tal:define-slot=header/div div id=content tal:define-slot=content/div div id=footer tal:define-slot=footer/div /body /html Every script then calls it's own template file (eg index.tpl) with tal:block metal:use-macro=general.tpl/globalpage tal:block metal:fill-slot=content Content of page goes here... /tal:block /tal:block If I have a page that needs to show different pages (eg: form + feedback after submission + error if required), eg register.tpl tal:block metal:use-macro=general.tpl/globalpage tal:block metal:fill-slot=content tal:condition=php: template=='form' form method=post username: input type=text name=username/ Pwd: input type=password name=pwd/ input type=submit name=submit value=Register/ /form /tal:block tal:block metal:fill-slot=content tal:condition=php: template=='succeed' your registration was successfull /tal:block tal:block metal:fill-slot=content tal:condition=php: template=='failed' your registration was not successfull. please try again. /tal:block /tal:block register.php would then set $phptal-template = 'succeed' etc to show the correct template... In these circumstances, using AJAX to retrieve only part of the page is harder. Do you have any feedback on that? For maintenance and support, I really really like to keep my templates grouped with the script. I know I could create a register_form.tpl, register_succeed.tpl, register_failed.tpl etc but this quickly becomes a cumbersome operation to open/close all those docs, let alone if you have a PHP script that runs through multiple, consequetive forms. On 18.09.2009, at 23:34, hosema...@poczta.onet.pl wrote: Hi I am working on ajax php website. Ajax makes using phptal more complicated. My approach is to divide single page to several php files and every time at single page i create new object of $tpl. It means that I have to use several $tpl-execute() at the first page load. I think it is not optimal. My question is whether there is most efective way to use phptal and ajax? page.xhtml: body stuff tal:block metal:use-macro=widget.xhtml/show_widget / stuff /body widget.xhtml: div metal:define-macro=show_widget ajax me! /div tal:block metal:use-macro=show_widget / This will let you either execute page.xhtml with all stuff in it, or execute widget.xhtml and just output that single fragment. You can use variables in use-macro (e.g. show_${name}), so you're not limited to one thing per file. ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] access previous / next item in array?
I'm doing a div tal:repeat=item items Previous id: ??? Current id: ${item/id} Next id: ??? /div Is there any way to add a callback function that will be called on each repeat, so I could easily put the next and previous id ? I could loop through the array in PHP, before assigning to PHPTal object, and set them. But that would mean looping through the array twice (once before, once for output)... ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re: [PHPTAL] PHPTAL prefilters
It sounds interesting and an improvement from previous versions. However, I'd like to see some tutorials putting this to work. When do you use those things, how can they be applied to improve your templates and/or code etc... What PHPTal lacks is this kind of documentation - the manual is fine, but there are no hands-on-tutorials available. Wednesday, September 16, 2009, 1:41:30 AM, you wrote: I think I've finally figured out decent design for filters in PHPTAL! Implementation is not finished yet! But have a look at the source of PHPTAL/PreFilter.php: http://phptal.org/files/PHPTAL-1.2.1b3.tar.gz There's base abstract class for prefilters. Previously you had to implement interface, now you'll have to extend the class. This change allows me to add new ways of filtering in the future without any breaking changes in the interface. The class has methods like filterDOM($node), filter($source) — you just override which ones you want. You can override all of them at once if you need, all will be called. DOM version lets you edit PHPTAL DOM nodes after they've been parsed and edit exactly what PHPTAL sees. There's also filterDOMFragment($node) method. I plan to add phptal:filter=name_of_the_filter attribute, which will let you apply filters selectively, e.g. (this is not implemented yet) ul phptal:filter=remove_whitespace liIE hates newlines in lists/li /ul script phptal:filter=minify_javascript etc. /script I've replaced PHPTAL-setPreFilter($filter) with PHPTAL-addPreFilter ($filter, $name = null). Yes, this means you can have any number of filters without workarounds (finally! :) I wonder if there should be separate method for adding named prefilters for use with phptal:filter only? (e.g. you might want to make JS minification prefilter available, but you don't want to minify entire file). Currently I expect this to be handled within filter, i.e. filter would implement filterDOMFragment() only, and have filterDOM() do nothing. Is that easy to use? Powerful enough? What do you think? ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] get raw version of template?
I use the PHPTal-templates also to fetch the (text) content of mails. Is there a way to get the plain-text version of a template? I'd like it to have the exact spacing indention... I would have expected to find a plain-text as setOutputMode()-param, but this doesn't exist ? ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] javascript code messed up
I'm using a tal:block fill-slot=script to fill my macro template with javascript code. However, all are converted to $lt;, which (seems to) cause an error in a for loop for (i=0; i myarray.lenght; i++) { ... } is converted to for (i=0; i lt; myarray.lenght; i++) { ... } and I get an error at that line. Any way to enforce phptal to put the content from the tal:block to be passed as text ? I tried specifying tal:block define-slot=structure script, but that doesn't work... ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re[2]: [PHPTAL] javascript code messed up
sorry, I overlooked your answer here. I was using PHPTal in default output mode, I was not specifying anything. I'm now specifying $phpTal-setOutputMode(PHPTAL::XHTML); My templates have doctype xhtml 1.0 Adding the /*![CDATA[*/ … /*]]*/ works fine (adding !-- -- too, at least in FF) thx! On 02-09-2009 at 14:04:15 GRolf ger...@pictureparking.com wrote: I'm using a tal:block fill-slot=script to fill my macro template with javascript code. However, all are converted to $lt;, which (seems to) cause an error in a for loop for (i=0; i myarray.lenght; i++) { ... } is converted to for (i=0; i lt; myarray.lenght; i++) { ... } and I get an error at that line. Apparently you're using text/html mode. Do you use HTML 5 output mode or XHTML? XHTML requires to be escaped as lt; (otherwise it would be ill-formed start of a tag). HTML requires the opposite - it has hardcoded rule that script content is CDATA where entities aren't interpreted. Any way to enforce phptal to put the content from the tal:block to be passed as text ? Strictly speaking it is passed as text, and text in markup has to be escaped. Try wrapping script in fill-slot in /*![CDATA[*/ … /*]]*/. That is a workaround for XHTML served as HTML. ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] tal:condition
the docs say about tal:condition that the block will only be shown if the expression evaluates to true... but I don't find the info if I couldn't easily compare a var to a string? should I always revert to php: stringname == 'value' ? Something like div class=errormsg tal:condition=errmsg:'unknonwn_user' Your username is unknown/div ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] nested tal:fill-slots ?
wondering about yet another thing is it somehow possible to nest a define-slot in a fill-slot ? I have tal:block metal:define-macro=globalpage ...header etc... tal:block metal:define-slot=main-content/ ...footer etc... /tal:block and tal:block metal:use-macro=globalpage tal:block metal:fill-slot=main-content ...main content goes here... ... would like to define a new slot here ... is that possible? /tal:block /tal:block ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re[2]: [PHPTAL] best practise ?
I tried that too - however, the inserted html is escaped so the are htmlentities Vriendelijke groeten, Tom De Bruyne -- More Fast BVBA - Management Tool Development E-mail: i...@morefast.be Tel: 0477 77 53 52 Thursday, August 27, 2009, 10:42:30 PM, you wrote: Hi, I've never been a big fan of metal macros so I might be a bit biased here. What I would do is instead of creating a slot for the header, populate it in your general.tpl file directly. Since you're already including a file in your pages to calculate the header contents, you could put there the tal instructions to fill up the header directly in the general template. /imv On 8/27/09 8:36 PM, GRolf wrote: I'm building a website and I'm wondering about the best approach ... The goal is the following: there is a general design of every page (header, main, footer). The content of the header changes based on the visitors adminlevel (nearly every visitor has something else in the header) - the exact content of this header is retrieved and parsed in PHP. The other parts of the page (eg 'main'), depend on the PHP page and can be handled by using a different file in setTemplate(). What I currently do: * I have a 'general.tpl' file, which contains html tal:define-macro=generalstructure body div id=headertal:block tal:define-slot=header//div div id=maintal:block tal:define-slot=main//div div id=footertal:block tal:define-slot=footer//div /body /html * every PHP file (index.php, register.php etc) calls it's own script file (index.tpl, register.tpl etc) via $phptal-setTemplate(). Each file contains something like tal:block metal:use-macro=general.tpl/generalstructure tal:block metal:fill-slot=main I'm index.php ... /tal:block /tal:block * every page (index.php etc) also contains included code to retrieve the header content (retrieve_header($user_id)); How can I now best ensure that this header content is nicely parsed into the header-slot, without having to put metal:fill-slot code in each and every page template ? Could somebody please give me an idea of PHPTal is supposed to handle this kind of situations? If you have any other suggestions how to improve this setup, be my guest ... ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
[PHPTAL] best practise ?
I'm building a website and I'm wondering about the best approach ... The goal is the following: there is a general design of every page (header, main, footer). The content of the header changes based on the visitors adminlevel (nearly every visitor has something else in the header) - the exact content of this header is retrieved and parsed in PHP. The other parts of the page (eg 'main'), depend on the PHP page and can be handled by using a different file in setTemplate(). What I currently do: * I have a 'general.tpl' file, which contains html tal:define-macro=generalstructure body div id=headertal:block tal:define-slot=header//div div id=maintal:block tal:define-slot=main//div div id=footertal:block tal:define-slot=footer//div /body /html * every PHP file (index.php, register.php etc) calls it's own script file (index.tpl, register.tpl etc) via $phptal-setTemplate(). Each file contains something like tal:block metal:use-macro=general.tpl/generalstructure tal:block metal:fill-slot=main I'm index.php ... /tal:block /tal:block * every page (index.php etc) also contains included code to retrieve the header content (retrieve_header($user_id)); How can I now best ensure that this header content is nicely parsed into the header-slot, without having to put metal:fill-slot code in each and every page template ? Could somebody please give me an idea of PHPTal is supposed to handle this kind of situations? If you have any other suggestions how to improve this setup, be my guest ... ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal
Re[2]: [PHPTAL] no error when var is not set ?
and how could I apply this to a tal:repeat ? I'd like to include some references to external javascriptfiles, if they were set. For that, I created the following lines in my template... tal:block tal:repeat=jsfile javascriptfiles script type=text/javascript src=${jsfile}/script /tal:block however, if no javascriptfiles are set, this doesn't work. I tried with tal:block tal:condition=exists javascriptfiles tal:repeat=jsfile javascriptfiles script type=text/javascript src=${jsfile}/script /tal:block but that also gives an error. Am I taking the wrong approach, or is there a solution? I also like using the solution that Levi suggests. Just note that the element's default content will be used if it is defined - this has the added value of being able to view your template (e.g) in your browser, without any replaced variables. So, note that this will output nothing if pagetitle is not assigned: title tal:content=pagetitle|default/title However, this will output Some Page Title if pagetitle is not assigned: title tal:content=pagetitle|defaultSome Page Title/title Regards, Werner Levi Stanley wrote: One solution is to do this: title tal:content=pagetitle | default/title GRolf wrote: When I create a simple template, but don't assign a value to all variables in the template, PHPTal throws an error. Is there a way to prevent this? I'd like to create some placeholders, where I can set variables, but they won't all be filled every time. e.g. head title tal:content=pagetitle/title /head It should also be possible to render this page without setting $title... ___ PHPTAL mailing listphp...@lists.motion-twin.comhttp://lists.motion-twin.com/mailman/listinfo/phptal ___ PHPTAL mailing listphp...@lists.motion-twin.comhttp://lists.motion-twin.com/mailman/listinfo/phptal ___ PHPTAL mailing list PHPTAL@lists.motion-twin.com http://lists.motion-twin.com/mailman/listinfo/phptal