rendering two lists within groovy template for play framework 1.2+ - playframework-1.x

I am trying to render a list within a list while using the groovy template for play framework 1.2+. However, when I use the #{list} tag within a #{list} tag, I do not get the desired result. Can anyone share how they have approached this in while using the groovy template for play (whether using Groovy or CSS). Thanks in advance

I created a little sample project with Play 1.2.5 and modified these two files:
Application.java:
public class Application extends Controller {
public static void index() {
List<List<String>> listWithLists = new ArrayList<List<String>>();
List<String> listInList1 = new ArrayList<String>();
listInList1.add("1st element in 1st list");
listInList1.add("2nd element in 1st list");
listWithLists.add(listInList1);
List<String> listInList2 = new ArrayList<String>();
listInList2.add("1st element in 2nd list");
listInList2.add("2nd element in 2nd list");
listWithLists.add(listInList2);
render(listWithLists);
}
}
index.html:
<ul>
#{list items:listWithLists, as:'listWithinList'}
#{list items:listWithinList, as:'string'}
<li>${string}</li>
#{/list}
#{/list}
</ul>

Related

Sitecore Building a single page from child node data

I have a Sitecore structure of items which comprises of
Range
Product 1
Product Name (text)
Product Image (image)
Product 2
Product Name (text)
Product Image (text)
I need to make a single page view that iterates through each of these nodes and collects and outputs the data for each - can anyone assist in the method I would best use to do this?
Sorry if this is a basic question but any example code would be appreciated.
You should really think about just using a single product template with a Product Name field and a Product Image field instead of having items with single fields under the product. But if this is your requirement, this is how you would do it.
SubLayout (or layout if need be)
<div>
<sc:Text runat="server" id="txtProductName" Field="ProductName"/>
<sc:Image runat="server" id="imgProductImage" Field="ProductImage"/>
</div>
Then in code behind you would take the current item (the product and find the child item that corresponds to what you are looking for and assign it as the field item.
private void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
txtProductName.Item = Sitecore.Context.Item.Children.FirstOrDefault(x => x.TemplateID == Constants.Templates.ProductName);
imgProductImage.Item = Sitecore.Context.Item.Children.FirstOrDefault(x => x.TemplateID == Constants.Templates.ProductImage);
}
In my example I am resolving it looking for an item of template type X, but you could go by name or some other way of knowing.

How to push an object into a one dimensional array and how to get individual item in array using ember.js

Here is my code, I want to get userData details it is in the form of array,so i push that object into results array, am getting userData items into my result array but i can't access the individual item ,and that i want to display in my template using {{#each}}{{/each}}, help me to find this.
enter code here
addSuccess: function(userData) {
Ember.Logger.log('user address data',userData);
var results = [];
results.push(userData);
this.set('result',results);
Ember.Logger.log('result data result',this.get('result.addresses.city'));
this.send('btnClose');
}
If I understand your question correctly, userdata is a plain object. Since Ember 2.1 you could iterate over an object with the each-in helper. So your code would look something like:
Controller/Route
addSuccess: function(userData) {
this.set('controller.userData', userData);
}
Template
<ul>
{{#each-in userData as |key value|}}
<li>{{key}}: {{value}}</li>
{{/each-in}}
</ul>

render sitecore mvc renderings programmatically

I want to get all the renderings of a content item and render each of them to html string inside an MVC action using C# code. Below is the code I am using to get all the renderings of a content item.
Item item = Sitecore.Context.Database.GetItem(someItem);
RenderingReference[] myRenderings = item.Visualization.GetRenderings(Sitecore.Context.Device, true);
foreach (RenderingReference rendering in myRenderings)
{
RenderingItem renderingItem = rendering.RenderingItem;
}
I am able to get this list and the rendering item's Id. But how can i render them to html string here?
Note: the renderings could be of any rendering type like view renderings, xsl renderings , controller renderings etc. I dont want to use the approach of WebClient or HtmlAgility pack.
Not sure what you really try to do, but this code allows you to render a rendering in a MVC action and returns the Html for this:
public ActionResult GetFirstRenderingHtml()
{
var rendering = PageContext.Current.PageDefinition.Renderings.First();
return this.View(new RenderingView(rendering));
}
This actually only returns the code of the first rendering of the current item. If you want to call this action directly, you need to add the sc_itemid parameter in the to specify the context item:
/api/yourcontroller/GetFirstRenderingHtml?sc_itemid=<item id>
If you want the Html for a complete item with all it's renderings, I suggest you create a web request to get the output.

Sencha Touch List Component

I have 2 questions. First, is this possible to use List item in sencha touch without template? I mean, can i access the list item seperately?
The second one is related with the first one. I need to send my store item in template as a function parametere how can i do this? I tried below but does not work.
template = new Ext.XTemplate(
'<div style="width:100%;height:40px;">',
'<span>{c}</span><span style="font-size:10px" >{e}</span>',
'<span> ',
'<tpl for=".">',
'<span onload="formatNumber({i})">{i}</span>',
'</tpl></span>',
'</div>',
{
Thanks,
The list items' HTML is in the property all.elements. That will give you the HTML DOM node of each item in the list. The property isn't public so it may break in a future version.
I don't really understand what you are trying to do. You can render anything with a template.

How to do JPA query from view template in play

I'm trying to do a JPA query from a view template but it's not working (I've verified that there are records using phpMyAdmin). I know this should normally be done through the controller and passed in via render, but this is part of building a menu which will appear on every page and I don't want to have to modify every controller to accomplish this.
What I'm currently trying is
<ul>
%{
import models.Building;
List<Building> buildings = Building.findAll();
}%
#{list items: buildings, as: 'building'}
<li>${building}</li>
#{/list}
</ul>
but I'm getting the error The template /app/views/Networks/grid.html does not compile : unexpected token: ( referencing the line which calls findAll(). What's the right way to do this?
Instead of trying to do this in the page (bad practice) or add it to every Controller you should add it to one parent controller in a method annotated with #Before. This will get called on each page so you only need to do the code once.
Eg. The parent controller (aka interceptor) would look like:
public class ControllerInterceptor extends Controller {
#Before
public static void intercept() {
RenderArgs.current().put("buildings", Building.findAll());
}
}
Then each controller would add the following annotation:
#With(ControllerInterceptor.class)
public class MyController extends Controller {
...
}
And your page code would then refer to it much as you're already doing:
<ul>
#{list buildings, as: 'building'}
<li>#{a #Buildings.edit(building.code)}${building}#{/a}</li>
#{/list}
</ul>
As for why your original code didn't work, I'm not sure. Possibly something to do with how the Model class is enhanced by Play?
Discovered how to work around it, but I'd still be interested to know what was wrong with the original code. I got it working by just doing
<ul>
#{list items: models.Building.findAll(), as: 'building'}
<li>#{a #Buildings.edit(building.code)}${building}#{/a}</li>
#{/list}
</ul>