AW: [opencms-dev] how to call a content file from a page ?

Michael Emmerich Michael.Emmerich at framfab.de
Fri Jan 11 17:28:49 CET 2002


Hello Bruno,

there is a very simple solution for your problem - use an element.

When you use an element in your content template, this element is always
running in the context of the page in which it was included.
So if you read the current folder from the element, you will get different
results when you use the element in foldera or folderb in your example.
Therefore you can access different fieles form the same element when it is
used in different pages.

Here is a little example how this could be done:

The element (/content/elements/content) could look like this:

<?xml version="1.0"?>
<XMLTEMPLATE>
<TEMPLATE>
<![CDATA[
This is the text: ]]><process>text</process><![CDATA[ <br>
This is the image: <img src="]]><process>image</process><![CDATA[">
]]>
</TEMPLATE>
</XMLTEMPLATE>


A simple sample content template which uses this template is the following:

<?xml version="1.0"?>
<XMLTEMPLATE>
<TEMPLATE>
<ELEMENT name="content"/>
<ELEMENT name="body"/>
</TEMPLATE>

<ELEMENTDEF name="content">
<class>de.framfab.opencms.modules.example.test</class>
<template>/content/elements/content</template>
</ELEMENTDEF>
</XMLTEMPLATE>


Now the intersting part is the required Java class which is used to fill the
element with its content:

package de.framfab.opencms.modules.example;

import com.opencms.template.CmsXmlTemplate;
import com.opencms.file.*;
import com.opencms.core.*;
import com.opencms.util.*;
import com.opencms.template.*;
import com.opencms.workplace.*;
import com.opencms.defaults.*;

import java.util.*;
import java.io.*;
import java.sql.*;
import java.text.*;

public class test extends CmsXmlTemplate {

 public byte[] getContent(CmsObject cms, String templateFile, String
elementName, Hashtable parameters, String templateSelector)
  throws CmsException{

  //get the element template
  CmsXmlTemplateFile templateDocument=getOwnTemplateFile(cms, templateFile,
elementName, parameters, templateSelector);

  // get the right path to the "ContentDefintion" file
  // in this example, its always the file "cd.xml" in the current folder
  CmsFolder folder=cms.getRequestContext().currentFolder();
  String contentFile=folder.getAbsolutePath()+"cd.xml";

  // now get the "ContentDefinition" file
  CmsXmlTemplateFile content=new CmsXmlTemplateFile(cms,contentFile);

  //read the requred datablocks from the "ContentDefintion" file
  String text=content.getDataValue("text");
  String image=content.getDataValue("image");

  // finally, insert those values into the element template
  templateDocument.setData("text",text);
  templateDocument.setData("image",image);

  // now process the element
  return startProcessing(cms, templateDocument, elementName, parameters,
templateSelector);
  }
}

Please note that is a quick and dirty example :)

This class first reads the element template. Then it reads the additional
xml file which defines the content (text & image) that should be inserted
into this template.
After the data is read form the "ContentDefintion" file it is set into the
datablocks of the element template and the output is generated.

The last thing that is missing is the cd.xml file which defined the content
that should be inserted:

<?xml version="1.0"?>
<XMLTEMPLATE>
<text>This is a test text</text>
<image>/pics/imgage.jpg</image>
</XMLTEMPLATE>

With this example, you would get a different content for your element in
different folders.

I hope this will help you with your problem.

Regards,
	Michael


-----Urspr√ľngliche Nachricht-----
Von: CALI-GARSIA Bruno [mailto:bruno.cali-garsia-renexter at renault.com]
Gesendet: Freitag, 11. Januar 2002 14:16
An: opencms-dev at www.opencms.com
Betreff: [opencms-dev] how to call a content file from a page ?


Hello,

I'm working on selecting dynamic content in a page: here is my problem :

I have 2 identical structures of folders:
folder1
  foldera
    page1 (<process> image </process>)
    image1
folder2
  folderb
    page2  (<process> image </process>)
    image2

The presentation of the page1 and page2 are strictly identical, except for
the images and texts
I wish, maybe is there another possibility (?), to put the definition of the
dynamic content(text and images) in one file at the level of the page:

ContentDefinition1.xml
<text> <![CDATA[ my text 1 ]]></text>
<image><![CDATA[/images/myimage1.gif]]></image>

ContentDefinition2.xml
<text> <![CDATA[ my text 2 ]]></text>
<image><![CDATA[/images/myimage2.gif]]></image>

My question is : where and how can I call theses file ?
I can't call it from the body of my page (because body pages can't call or
define any contenttemplate, element or element tag(??))
I can't call it from my elements because they are common to several pages
I can't call it from the frametemplate because the presentation of the
elements (or contenttemplates) is also common
I can't call it neither from the mastertemplate because I should write one
master template for every pages (800 pages !)

Any idea on how to do it ? Should I write my own class which get the
navigation context and put the good image and text ?
Please, help !

Greetings
Bruno




More information about the opencms-dev mailing list