Hallo Bob,
here some ideas to problem 1:
Do you really need different HTML structures in the various contexts of your
paragraphs? If not you may consider solving this problem with CSS.
If CSS cannot do the job you may need subtemplates for paragraphs.
Unfortunately magnolia STK supports subtemplating for templates only, but not
for paragraphs.
To overcome this limitation I extended the STK Paragraph class to use a
configured map of views (ftl templates) and a view resolver strategy that gets
the view key from somewhere (MgnlContext...).
public class MultiViewParagraph extends Paragraph {
private static final Logger LOG =
LoggerFactory.getLogger(MultiViewParagraph.class);
private Map<String, String> _views = Collections.emptyMap();
private ViewResolver _viewResolver = new DefaultViewResolver();
@Override
public String determineTemplatePath(String actionResult, RenderingModel
model) {
String result = super.determineTemplatePath(actionResult, model);
if (_views != null && !_views.isEmpty()) {
result = _viewResolver.getView(_views, result);
}
return result;
}
public void setViewResolverClass(Class<? extends ViewResolver>
viewResolverClass) {
try {
_viewResolver = viewResolverClass.newInstance();
} catch (InstantiationException e) {
LOG.error("Failed to instantiate ViewResolver: " +
viewResolverClass.getName(), e);
throw new RuntimeException("Cannot instantiate ViewResolver " +
viewResolverClass, e);
} catch (IllegalAccessException e) {
LOG.error("Failed to instantiate ViewResolver: " +
viewResolverClass.getName(), e);
throw new RuntimeException("Cannot instantiate ViewResolver " +
viewResolverClass, e);
}
}
public void setViewResolver(ViewResolver viewResolver) {
_viewResolver = viewResolver;
}
public void setViews(Map<String, String> views) {
_views = views;
}
...
Using a ViewResolver like
public class ContextViewResolver extends DefaultViewResolver {
@Override
protected String getViewKey() {
return (String) MgnlContext.getAttribute(VIEW_SELECTOR_KEY,
Context.LOCAL_SCOPE);
}
}
And
public class DefaultViewResolver implements ViewResolver {
@Override
public String getView(Map<String, String> views, String defaultView) {
String key = getViewKey();
String result = null;
if (isNotBlank(key) && views != null) {
result = views.get(key);
}
return isNotBlank(result) ? result : defaultView;
}
protected String getViewKey() {
return EMPTY;
}
@Override
public String getView(Map<String, String> views) {
return getView(views, null);
}
}
In the paragraph configuration replace the
info.magnolia.module.templating.Paragraph by the MultiViewParagraph and
provide the key-ftl-Map (node views) and the class of the desired ViewResolver
implementation (nodeData viewResolverClass).
In your ftl-Templates set the view-key as context attribute before including
the paragraph. You may consider extending the [@cms.includeTemplate..] Tag
class to accept the view key as attribute and set it as MgnlContext attribute.
Hope that helps.
Wolf
-----Ursprüngliche Nachricht-----
Von: [email protected] [mailto:[email protected]]
Im Auftrag von Magnolia Forums (on behalf of Robert Bowen)
Gesendet: Mittwoch, 20. Juli 2011 01:08
An: Magnolia User List
Betreff: [magnolia-user] Tips for How to Creat a News Section Without STK
I have a typical News dialog with title, subtitle, lead text, text, and a
couple images. This information appears in various pages in various formats:
In the home page the 2 newest news items appear with photo, title and subtitle.
In the news list page all (or many) news items appear with image, title and
lead text.
In the news item page one news item appears with ... well, everything.
Questions:
1. What's the best way to do this. I started out doing this in the Home page:
[#assign newsItems=mgnl.inherit(content).homePageNewsCollection /] [#list
newItems?children! as kid]
[@cms.includeTemplate contentNode=kid /] [/#list] [@cms.newBar
contentNodeCollectionName="homePageNewsCollection" paragraph="homePageNews" /]
... which works fine - the 2 newest news items appear with photo, title and
subtitle..
But I tried doing the same thing in a different page. And it prints the data
but with the same format as the home page! In my Magnolia ignorance I didn't
realize that when you save the date you are also saving its paragraph type with
it.
If I change the new bar line to this:
[@cms.newBar contentNodeCollectionName="homePageNewsCollection"
paragraph="newsListPageNews" /]
... any new data entered will be displayed correctly on the news list page. But
the old ones are still displayed with the home page format. Furthermore, that
new data will be shown on the home page with the news list format!
The same goes for the news item page.
All of this prolog is to ask: how do you display the same info in different
formats (using different paragraphs, all linked to the same dialog) on
different pages?
For the time being I am doing it like this, for example in my news list page:
[#assign newsItems=mgnl.inherit(content).homePageNewsCollection /] [#list
newItems?children! as kid]
${kid.title}<br>
${kid.subTitle}
[/#list]
... but I am not sure if this is a good idea.
2. How do you access the JCR id for an item? I would like to pass the id for a
given news item via the url and then only show the info for that news item.
Something like this:
[#assign newsItems=mgnl.inherit(content).homePageNewsCollection /] [#list
newItems?children! as kid]
[#if kid.id! == ctx.parameters.id!]
${kid.title}<br>
${kid.subTitle}
[/#if]
Is this the way to do it?
Sorry for the long post. All this stuff is already done in the STK, I realize I
am re-inventing the wheel. But I am not really sure how to see the 'source
code' (ftl files) for all the STK stuff.
Anyway, I am sure question 2 is simple enough I just can't find the answer.
Question 1 on the other hand ... not sure how to go about it.
Thanks!
Bob
[/#list]
--
Context is everything:
http://forum.magnolia-cms.com/forum/thread.html?threadId=79c9f527-c5e9-4eb9-b96f-019661451ba4
----------------------------------------------------------------
For list details see
http://www.magnolia-cms.com/community/mailing-lists.html
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------
----------------------------------------------------------------
For list details see
http://www.magnolia-cms.com/community/mailing-lists.html
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------