[opencms-dev] Extracting body content items from a page type
Millo Magnocavallo
millo_magno at yahoo.com
Fri Sep 10 19:19:32 CEST 2004
Hi,
I've got multiple bodies content areas working on my site, So that when
a user edits the page they have not only a template drop down but also
a bodies drop down which divides the page into various parts and allows
the user to add content to various locations on the template simply by
selecting the required body.
The template can then pull out the content which the user has entered
into each seperate body into various locations on the template using
the include() method from the JspCmsActionElement, for example if I
have the following bodies in my template:
feature1Copy, feature2Copy, feature3Copy, feature1Image, feature2Image,
feature3Image
I can display each of these bodies in different locations on the page
like so:
<table background="<% cmsJspAE.include(null, "feature1Image"); %>">
<td>
<% cmsJspAE.include(null, "feature1Copy"); %>
</td>
</table>
However, here comes the problems and perhaps you can see the problem
that I face already. The images that the user uploads are supposed to
appear as background images and things like a table background do not
take a full img tag as it's value (<img alt=blah src="whatever.gif">)
which is what is output. It just needs a source path which is exactly
what I wish to extract from the user uploaded image.
So what I need is to be able to manipulate the output, however because
this is done with includes, it is impossible because include() methods
return void and leaves you with nothing to manipulate, it simply
includes a page and that's all it needs to do.
I've found most of what was needed in the book written by Matt Butcher
here is the code:
String fileName = cmsJspAE.getRequestContext().getUri();
com.opencms.file.CmsObject cmsObj = cmsJspAE.getCmsObject();
com.opencms.file.CmsFile cmsFile = cmsObj.readFile(fileName);
if (cmsFile.getType() ==
cmsObj.getResourceType("page").getResourceType()) {
String fileTitle = cmsObj.readProperty(fileName, "title");
com.opencms.template.CmsXmlControlFile controlCode = new
com.opencms.template.CmsXmlControlFile(cmsObj,cmsFile);
String bodyFileName = controlCode.getElementTemplate("body");
com.opencms.template.CmsXmlTemplateFile bodyFile = new
com.opencms.template.CmsXmlTemplateFile(cmsObj,bodyFileName);
// assign appropriate values to page field variables
feature1Copy = bodyFile.getTemplateContent(null,null,"feature1Copy");
feature1Image =
bodyFile.getTemplateContent(null,null,"feature1Image");
feature2Copy = bodyFile.getTemplateContent(null,null,"feature2Copy");
feature2Image =
bodyFile.getTemplateContent(null,null,"feature2Image");
feature3Copy = bodyFile.getTemplateContent(null,null,"feature3Copy");
feature3Image =
bodyFile.getTemplateContent(null,null,"feature3Image");
}
If I look at the body file (/system/bodies/mysite/index.html) after
having uploaded images and copy etc it looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<XMLTEMPLATE>
<TEMPLATE/>
<EDITTEMPLATE><![CDATA[
]]></EDITTEMPLATE>
<TEMPLATE name="feature1Copy"><![CDATA[feature1 copy goes
here]]></TEMPLATE>
<EDITTEMPLATE name="feature1Copy"><![CDATA[feature1 copy goes
here]]></EDITTEMPLATE>
<TEMPLATE name="feature1Image"><![CDATA[<img
alt="programmes-std-thumb"
src="]]><LINK><![CDATA[/system/galleries/pics/mysite/tmp/programmes-std-thumb.jpg]]></LINK><![CDATA[">]]></TEMPLATE>
<EDITTEMPLATE name="feature1Image"><![CDATA[<IMG
alt=programmes-std-thumb
src="${OpenCmsContext}/system/galleries/pics/mysite/tmp/programmes-std-thumb.jpg">]]></EDITTEMPLATE>
<TEMPLATE name="feature2Copy"><![CDATA[feature2 copy goes
here]]></TEMPLATE>
<EDITTEMPLATE name="feature2Copy"><![CDATA[ feature2 copy goes here
]]></EDITTEMPLATE>
<TEMPLATE name="feature2Image"><![CDATA[<img
alt="programmes-std-thumb"
src="]]><LINK><![CDATA[/system/galleries/pics/mysite/tmp/programmes-std-thumb.jpg]]></LINK><![CDATA[">]]></TEMPLATE>
<EDITTEMPLATE name="feature2Image"><![CDATA[<IMG
alt=programmes-std-thumb
src="${OpenCmsContext}/system/galleries/pics/mysite/tmp/programmes-std-thumb.jpg">]]></EDITTEMPLATE>
<TEMPLATE name="feature3Copy"><![CDATA[feature3 copy goes
here]]></TEMPLATE>
<EDITTEMPLATE name="feature3Copy"><![CDATA[ feature3 copy goes here
]]></EDITTEMPLATE>
<TEMPLATE name="feature3Image"><![CDATA[<img
alt="programmes-std-thumb"
src="]]><LINK><![CDATA[/system/galleries/pics/mysite/tmp/programmes-std-thumb.jpg]]></LINK><![CDATA[">]]></TEMPLATE>
<EDITTEMPLATE name="feature3Image"><![CDATA[<IMG
alt=programmes-std-thumb
src="${OpenCmsContext}/system/galleries/pics/mysite/tmp/programmes-std-thumb.jpg">]]></EDITTEMPLATE>
</XMLTEMPLATE>
So theoretically I thought that the code would pull out something like
<img alt="programmes-std-thumb"
src="]]><LINK><![CDATA[/system/galleries/pics/mysite/tmp/programmes-std-thumb.jpg]]></LINK><![CDATA[">
or <IMG alt=programmes-std-thumb
src="${OpenCmsContext}/system/galleries/pics/mysite/tmp/programmes-std-thumb.jpg">
or basically something which had the img src in it but all I get is
this:
<img alt="feature1Image" src="">
I think that it's trying to extract the content from the TEMPLATE
rather than the EDITTEMPLATE, but either way, with respects to the
image bodies (feature1Image, feature2Image, feature3Image) they both
have funny characters after the 'src=' so I guess it's this that's
breaking it.
Is this a bug or is there a way around this so that it escapes the
special characters so I can get the full image tag?
Would love help on this one.
Thanks in advance.
Millo
More information about the opencms-dev
mailing list