Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
In-Template Function Definitions Question

 
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3
View previous topic :: View next topic  
Author Message
SoN9ne
Smarty Rookie


Joined: 25 Feb 2010
Posts: 17

PostPosted: Wed May 23, 2012 3:00 pm    Post subject: In-Template Function Definitions Question Reply with quote

Hey guys,

I took a break on my project and now I am back in full force...

My system allows for multiple themes depending on which system you are in. I also built my own form system that is built in PHP and the object is passed into Smaty and Smarty will build the form using object I have given it.

My question is a two-part question.

I find that using the In-Template Function Definitions very crucial in my development, especially since each theme will have a different function unique to the template.

So my first question is: Can you have more than one function defined in a TPL file? Every time I make a function under another function I get and error like: (it recognizes the first function but not the second)
Code:
Syntax Error in template "\application\template\grape\_objects\form.tpl" on line 5 "{buildForm T_FORM_OBJECT=$T_FORM_OBJECT}" unknown tag "buildForm"


Because of this, I made two separate files that were my functions and it seemed to work until I cleared my cache (the form function was not originally a function before I cleared the cache so maybe that is why it worked?)... Now I am back to getting the same error above. So I am thinking I cannot define functions like this or maybe I am missing something simple. Thought I would ask the gurus here for a little push in the right direction. Smile

My second question is about including files in the TPL file.

For Example: I have a toolbox.tpl and a form.tpl. You can see from the code below that I do includes on each tpl and I am wondering if there is a better way to handle this? Is this okay to do it this way or should I find a better solution?

It should be noted that the toolbox and the form may or maynot both be used on the same page.

I hope someone here can help push me in the right direction because I am not having luck on Google or using the search here.

Thanks in advance Very Happy

If it helps my file structure is:
Code:

- Template
    - styleName
        - _objects
            - buildFormFunction.tpl
            - formInputFieldFunction.tpl
            - form.tpl
            - toolbox.tpl
        - pages
            - pageName.tpl
        - widgets
            - widgetname.tpl
        - header.tpl
        - footer.tpl
        - sidebar.tpl
        - topbar.tpl


toolbox.tpl
Code:
{function name="displayToolboxItem"}
<li>
    <a rel="tooltip" title="{$toolboxItem['form']->getTitle()}" class="toolbox-action" href="javascript:void(0);"><span class="{$toolboxItem['iconClassName']}"></span></a>
    <div class="toolbox-content">
        <div class="block-border form-cage isToolbox">
            {buildForm T_FORM_OBJECT=$toolboxItem['form'] isToolbox=true}
        </div>
    </div>
</li>
{/function}
{include file="_objects/formInputFieldFunction.tpl"}
{include file="_objects/buildFormFunction.tpl"}
{if ToolboxContainer::hasToolboxes()}
<ul class="toolbox-header">
    {foreach ToolboxContainer::getToolboxes() as $toolboxItem}
        {displayToolboxItem toolboxItem=$toolboxItem}
    {/foreach}
</ul>
{/if}


form.tpl
Code:
{include file="_objects/formInputFieldFunction.tpl"}
{include file="_objects/buildFormFunction.tpl"}
<div class="grid_{$T_FORM_OBJECT->getContainerSize()} form-cage">
    <div class="block-border">
        {buildForm T_FORM_OBJECT=$T_FORM_OBJECT}
    </div>
</div>



buildFormFunction.tpl
Code:
{function name="buildForm" isToolbox=false}
    {if $T_FORM_OBJECT->getTitle()}
        <div class="block-header{if $isToolbox} small{/if}">
            <h1>{$T_FORM_OBJECT->getTitle()}</h1>{if !$isToolbox}<span></span>{/if}
        </div>
    {/if}
        <div class="block-content">
            {if $T_FORM_OBJECT->getDesc()}
<!--            <div class="alert info no-margin top">-->
                <p>{$T_FORM_OBJECT->getDesc()}</p>
<!--            </div>-->
            {/if}
            <div id="{$T_FORM_OBJECT->getID()}-response-placeholder"></div>
            <form {$T_FORM_OBJECT->getAttributesAsString()}>             
               
                {foreach $T_FORM_OBJECT->getHiddenElements() as $HTMLFormElement}
                        {formInputField formElementObject=$HTMLFormElement wrap=false}
                {/foreach}
               
                {foreach $T_FORM_OBJECT->getElements() as $fieldsetID => $formElementFieldsetElements}
                    <fieldset>
                        {if $T_FORM_OBJECT->getFieldset($fieldsetID)->getTitle()}
                            <legend>{$T_FORM_OBJECT->getFieldset($fieldsetID)->getTitle()}</legend>
                        {/if}
                        {foreach $formElementFieldsetElements as $HTMLFormElement}
                            <div{if $HTMLFormElement->getElementWidth()} class="{$HTMLFormElement->getElementWidth()}"{/if}>
                            {formInputField formElementObject=$HTMLFormElement isToolbox=true}
                            </div>
                        {/foreach}
                    </fieldset>
                {/foreach}
                   
                <div class="clear"></div>

                <!-- Buttons with actionbar  -->
                <div class="block-actions">
                     
                    <ul class="actions-left">
                {if $isToolbox}
                        <li><a class="close-toolbox button red" id="cancel" href="javascript:void(0);">Cancel</a></li>
                {/if}               
                {if $T_FORM_OBJECT->useResetButton()}
                        <li><input type="reset" class="button red" value="{$T_FORM_OBJECT->getResetText()}" title="{$T_FORM_OBJECT->getResetConfirmationText()}" /></li>
                {/if}                           
                    </ul>

                    <ul class="actions-right">
                        <li><input type="submit" class="button" value="{$T_FORM_OBJECT->getSubmitText()}" /></li>
                    </ul>
                </div> <!--! end of #block-actions -->
            </form>
        </div>
{/function}


formInputFunction.tpl
Code:
{function name="formInputField" level=1 wrap=true}
    {if $wrap && $formElementObject->getType() !== HTMLFormElement::TYPE_RADIO && $formElementObject->getType() !== HTMLFormElement::TYPE_CHECKBOX}
        <p>
    {/if}
        {if $formElementObject->useLabel()}
            <label>{$formElementObject->getLabel()->getTitle()}{if $formElementObject->isRequired()}&nbsp;<span class="required-icon">*</span>{/if}</label>
        {/if}

   {switch $formElementObject->getType()}
      {case HTMLFormElement::TYPE_TEXT}
      {case HTMLFormElement::TYPE_PASSWORD}
      {case HTMLFormElement::TYPE_FILE}
      {case HTMLFormElement::TYPE_BUTTON}
      {case HTMLFormElement::TYPE_IMAGE}
      {case HTMLFormElement::TYPE_HIDDEN}
         <input {$formElementObject->getAttributesAsString()} />      
         {/case}
      {case HTMLFormElement::TYPE_TEXTAREA}
         <textarea {$formElementObject->getAttributesAsString()}>{$formElementObject->getValue()}</textarea>      
         {/case}
      {case HTMLFormElement::TYPE_RADIO}
      {case HTMLFormElement::TYPE_CHECKBOX}
         <fieldset id="options-cage-{$formElementObject->getId()}" class="options-cage">
                            <legend title="{$formElementObject->getTitle()}">{$formElementObject->getTitle()}{if $formElementObject->isRequired()}&nbsp;<span class="required-icon">*</span>{/if}</legend>
         {foreach $formElementObject->getOptions() as $elementOptionObject}
            <label id="{$formElementObject->getId()}-{$elementOptionObject->getValue()}" {$elementOptionObject->getLabel()->getAttributesAsString()}>
               <input id="{$formElementObject->getId()}-{$elementOptionObject->getValue()}" name="{$formElementObject->getName()}{if $formElementObject->isMultiple()}[]{/if}"
                                               type="{if $formElementObject->getType() == HTMLFormElement::TYPE_RADIO}radio{else}checkbox{/if}"
                     {if $elementOptionObject->getValue() == $formElementObject->getValue()} checked="checked"{/if}
                     {$elementOptionObject->getAttributesAsString()} />&nbsp;{$elementOptionObject->getTitle()}
            </label>
            {if $formElementObject->hasInLineDependencies()}
                                    <div id="{$formElementObject->getId()}-{$elementOptionObject->getValue()}-dep" class="dependency">
                                        {foreach $elementOptionObject->getDependencies() as $tmpDependencyElementObject}
                                            {formInputField formElementObject=$tmpDependencyElementObject level=2}
                                        {/foreach}
                                    </div>
            {/if}
         {/foreach}
         </fieldset>
         {if !$formElementObject->hasInLineDependencies()}
            {foreach $formElementObject->getOptions() as $elementOptionObject}
            {if $elementOptionObject->getDependencies()}
               <div id="{$formElementObject->getId()}-{$elementOptionObject->getValue()}-dep" class="dependency">
               {foreach $elementOptionObject->getDependencies() as $tmpDependencyElementObject}
                                            {formInputField formElementObject=$tmpDependencyElementObject level=2}
               {/foreach}
               </div>
            {/if}
            {/foreach}
         {/if}      
         {/case}
      {case HTMLFormElement::TYPE_SELECT}
         <select class="input-select {$formElementObject->getClassAsString()}" {$formElementObject->getAttributesAsString()}>
            <option value="" title="{lang key="L_PLEASE_SELECT"}">- {lang key="L_PLEASE_SELECT"}</option>
         {foreach $formElementObject->getOptions() as $elementOptionObject}
            <option value="{$elementOptionObject->getValue()}" title="{$elementOptionObject->getTitle()}"{if $elementOptionObject->getValue() == $formElementObject->getValue()} selected="selected"{/if}>{$elementOptionObject->getTitle()}</option>
         {/foreach}
         </select>
         {foreach $formElementObject->getOptions() as $elementOptionObject}
            {if $elementOptionObject->getDependencies()}
               <div id="{$formElementObject->getId()}-{$elementOptionObject->getValue()}-dep" class="dependency">
               {foreach $elementOptionObject->getDependencies() as $tmpDependencyElementObject}
                                            {formInputField formElementObject=$tmpDependencyElementObject level=2}
               {/foreach}
               </div>
            {/if}
         {/foreach}      
         {/case}
   {/switch}
    {if $wrap}
        </p>
    {/if}

   {if $formElementObject->getDependencies()}
      <div id="{$formElementObject->getId()}-dep" class="dependency">
      {foreach $formElementObject->getDependencies() as $dependencyElementObject}
                    {formInputField formElementObject=$dependencyElementObject level=2}
      {/foreach}
      </div>
   {/if}
{/function}
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 4649
Location: Hamburg / Germany

PostPosted: Wed May 23, 2012 8:11 pm    Post subject: Reply with quote

Use {call} to call template functions which definened within other templates.

See http://www.smarty.net/docs/en/language.function.call.tpl
Back to top
View user's profile Send private message
SoN9ne
Smarty Rookie


Joined: 25 Feb 2010
Posts: 17

PostPosted: Fri May 25, 2012 9:53 pm    Post subject: Reply with quote

Thanks Very Happy Guess I overlooked that part of the manual... Embarassed
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Smarty 3 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP