Hello :)

Ok, well, I'm working on the Ztal project which is an integration of PHPTal and 
Zend framework. Part of this work involves replacing the extremely non-mvc 
Zend_Form implementation with something rather nicer. For our version of 
Zend_Form we have a set of macros that process the elements in a Zend_Form 
instance to create a form layout. One of the elements in a Zend_Form is a 
grouping element which we turn into a fieldset and we'd like to be able to 
inject blocks of static html potentially before and after this element to allow 
users to give titles, descriptions etc. to the fieldset. Each grouping element 
has a unique name (defined in the Zend_Form subclass by the user when they 
create it) and the idea was to setup the macro for handling the grouping 
element so it could check for and insert slot content based on the grouping 
element name (name-preSlot, name-postSlot). Slots are a very neat way to do 
this because it is easy to fill them within the form-processing-macro and they 
don't have to exist.

Obviously, because the existence, number and names of the grouping elements is 
not known at compile time, I cannot reference specific slots by name at compile 
time.

I've actually found an alternative solution using the following tale method 
which 'get's the slot content and pushes it into a variable which is then 
output using the 'structure' keyword:

        public static function getSlotContent($src, $nothrow)
        {
                $break = strpos($src, '|');
                if ($break === false) {
                        $slotName = $src;
                        $notTrue = 'NULL';
                } else {
                        $slotName = substr($src, 0, $break);
                        $notTrue = phptal_tales(substr($src, $break + 1), 
$nothrow);
                }
                return '($ctx->hasSlot(' . phptal_tale($slotName, $nothrow)
                        . ')?$ctx->getSlot(' . phptal_tale($slotName, $nothrow)
                        . '):' . $notTrue . ')';
        }

I use it as follows (within the zend form processing macros):

<span tal:replace="structure 
Ztal_Tales_Form.getSlotContent:string:${repeat/element/key}-preSlot" />


The experience of the end user hides all of this and they would just do 
something like:

        <tal:block tal:condition="exists:communicationForm" tal:define="form 
communicationForm" metal:use-macro="global/form.xhtml/talForm">
                <tal:block metal:fill-slot="contactTimePreference-preSlot">
                        <div>This set of preferences control when and how we 
contact you when we need to. </div>
                </tal:block>
        </tal:block>


Where the Zend_Form instance is communicationForm and the fieldset (zend form 
grouping element) has a name of contactTimePreference.

Robert

p.s. The Ztal codebase is available on github but it is still going through a 
little bit of flux as we modify it to work with phptal 1.2.2 and Zend 1.11.0 
and also remove some of the larger performance hits by changing how we link to 
Zend (replacing action helpers and resource helpers with a pre-dispatch plugin) 
:)

On 8 Nov 2010, at 12:38, GRolf wrote:

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

Attachment: PGP.sig
Description: This is a digitally signed message part

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

Reply via email to