Dashboard > RIFE > ... > Tips and Tricks > Default template associated with an element
RIFE Log In | Sign Up   View a printable version of the current page.
Default template associated with an element


Added by Frederic Daoud, last edited by Frederic Daoud on Sep 16, 2006
Labels: 
(None)

Here's a simple mechanism I like to use so that, by default, the template associated with an element is named the same as the element's class name. For example, an element defined in a class ViewPerson would automatically use the template ViewPerson.html. With this, all elements that extend the following BaseElement don't need to be configured for a template when this default is acceptable. Note that you can still override this default by injecting a different template.

Here is the BaseElement class:

BaseElement.java
import com.uwyn.rife.engine.Element;
import com.uwyn.rife.template.Template;
import com.uwyn.rife.tools.ClassUtils;
 
/**
 * Base element.
 */
public class BaseElement extends Element
{
    private Template m_template;
    public Template getTemplate() { return m_template; }
    public void setTemplate(Template p_template) { m_template = p_template; }
 
    /**
     * Called by the RIFE framework to initialize the element.
     */
    public void initialize()
    {
        if (m_template == null)
        {
            try
            {
                m_template =
                  getHtmlTemplate(ClassUtils.simpleClassName(getClass()));
            }
            catch (Exception exc)
            {
            }
        }
    }
}

As I mentioned, you can still use a different template if the default is not what you want. For example:

site.xml
<site>
  <element implementation="your.rife.elements.ViewPerson"/>

  <element id="AjaxViewPerson" url="/AjaxViewPerson" implementation="your.rife.elements.ViewPerson">
    <property name="template">
      <template type="xml">AjaxViewPerson</template>
    </property>
  </element>
</site>

In this example the URL /viewperson (the default) will use ViewPerson.html as a template, and /AjaxViewPerson will use the same element but output the results using AjaxViewPerson.xml.



Are you enjoying Confluence? Please consider purchasing it today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.1a Build:#515 May 19, 2006) - Bug/feature request - Contact Administrators