How to use SiteEdit in DD4T in Tridion - tridion

Can anyone direct me on how to use SiteEdit specific syntax or tags in the Razor template in DD4T code? I have seen Site Edit related classes in it, but could not understand how to use SiteEdit tags in the view where there were some syntax similar to Dreamweaver for example RenderComponentField or tcdl:ComponentField which we used in Dreamweaver. I am very new to DD4T and am using SiteEdit 2009 SP3 and SDL Tridon 2011 SP1.
Thanks in advance.
Update: We have tried the implementation mentioned in the answer but unfortunately we are getting an error when we are accessing the page in proxy stating "Invalid template -The HTML is invalid, probably because your template code produces invalid HTML, such as a p tag inside another p tag. Check your HTML using a validator such as the W3C Markup Validation Service, correct your template, and republish."
I guess this is because Xml content is being pushed into html page due to the use of Dynamic Page and Component TBBs which we used in our Component and Page Templates.
Let us kow how to continue further.

The SiteEdit markup is not generated like it is done in a Dreamweaver Template, you are responsible yourself to place the markup for your editable fields in the Razor view. Good thing for this is that it works for similar for SiteEdit 2009 SP3 and UI 2012. Downside for UI 2012 is that it is not listening (yet) to the Enable Inline Editing which you can set on the Component or Page Templates (something we should consider for a future DD4T version).
Everything is based on the DD4T SiteEditHelper class. You start in your page view (before the </body> tag) by placing the following call:
#Html.SiteEditPage(Model)
This will write out the page markup, and if you set the style to "SiteEdit2012" in your SiteEdit_config.xml it will also write out the bootstrap script required for UI 2012.
Then for every Component Presentation and every editable Component Field you will also need to add the appropriate markup. For a Component presentation you can use:
#{var ComponentPresentation = ViewBag.ComponentPresentation as IComponentPresentation;}
<div>
#Html.SiteEditComponentPresentation(ComponentPresentation)
</div>
Make sure you write this out inside a DIV or some other element that can mark the boundary of your Component Presentation. For Component Fields a similar story, you can use:
<div>
#Html.SiteEditField(Model, Model.Fields["FieldName"])
#Model.Fields["FieldName"].Value
</div>

Apart from the changes to your views, as Bart has described, you also need to put a configuration file in the root of your web application, called SiteEdit_config.xml. It should look like this:
<?xml version="1.0" encoding="utf-8" ?>
<siteEdit enabled="true" tridionHostUrl="http://tridion.my.com">
<contextPublications>
<contextPublication id="10" componentPublication="3" pagePublication="9" publishPublication="10" />
<contextPublication id="11" componentPublication="3" pagePublication="9" publishPublication="11" />
</contextPublications>
</siteEdit>
You must list all your active publications here. DD4T tries to match your current page to the correct context publication (based on the 'id' attribute). If it cannot find it, SiteEdit will be disabled. The other attributes allow you to control the behaviour of SiteEdit.
componentPublication: new components will be created here
pagePublication: new pages will be created here (not used in Tridion UI 2012)
publishPublication: pages and components will be republished from this context (usually coincides with the id)
DD4T can also easily be configured for use with Tridion UI 2012. Just change the first element in the configuration as follows:
<?xml version="1.0" encoding="utf-8" ?>
<siteEdit enabled="true" style="SiteEdit2012" tridionHostUrl="http://tridion.my.com">
...
</siteEdit>

Related

Using several RenderComponentPresentations for XPM templates w/ Razor

As you probably already know, I'm working hard on some XPM templates with Razor. I've ran into another issue, this time concerning rendering components inside templates in order to make them siteEditable.
The following I'm not sure about. I've got a component which has a title field called "Title", and multivalue componentlink fields which consists of components with a title, description and image. Let's call this one "Linked USP" for now.
Currently this is being rendered by a Template called 'Page Banner', and it just iterates over fields with some If-loops to determine it's presentation, especially for the Title. In order for XPM to work, this template needs to render the component presentation of "Linked USP". So we've created a template called "[XPM] USP ITEM". - this Component template has the 'enable content editing TBB" added to it.
Now the issue arises when I want to make the Title Editable as well. Sounds straightforward, no? Not really - because when the parent template has got a "enable for content editing" TBB added, it will add <span> tags to all editable fields but the templates that gets invoked inside this template will also have <span> -tags, effectively bloating the html and making it impossible to edit the fields inside the RenderComponentPresentation because of duplicate <span>s.
Some code for your fun and to illustrate my issue:
<h1>#RenderComponentField("Title", 0)</h1>
#Foreach(var linkedUSP in Fields.USPS){
#RenderComponentPresentation("linkedUSP.ID", "tcm:10-1076-32")
}
This template has an enable Content edit TBB added.
now, for the RCP mentioned above, inside its [XPM] template:
<div class="title">#RenderComponentField("Title", 0)</div>
<p>#RenderComponentField("Description", 0)</p>
<tcdl:ComponentField name="img">
<img src="#img" alt="img.MetaData.alt">
</tcdl:ComponentField>
This one ALSO has the "Enable Content Edit" TBB added. On the front end this happens:
<div class="title"><span><span>Men</span></span></div>
Because the parent template also adds spans to the field.
So my question: how do i solve this? The Title field mentioned above has to be inside the parent template, but I can't create a special template for it becuase it is no component link. I can't get the TBB out of my RCP template, because it won't be editable that way. Interesting huh?
Can't I disable the spans inside template builder somehow?

Should I use tcdl, Tridion tag, or RenderComponentPresentation?

Managing a "Global" Logo with Tridion
To manage, for example, a website-wide logo and its text elements with SDL Tridion, I'd want it in a component, rendered by a (dynamic) component template, and placed by page templates. My requirements would include:
Logo as a Component--image as a multimedia (mm) component and link, alt (i.e. "home"), and other text in an optional container component
Dynamic Component Presentation (DCP)--ability to manage logo's markup presentation and publish changes without requiring publishing every page
Share-able--ability to localize multimedia and text in components, rather than templates, in lower publications
Authors shouldn't need to place the logo as a component presentation (CP) on every page, although a logo CP just on the homepage could offer dynamic linking.
RenderComponentPresentation(), TCDL, or Tridion tags for "Non CPs"
For this CP-less approach, I think RenderComponentPresentation() or TCDL tag would be the right "agnostic" approach. Is one preferred over the other?
For example, my template outputs:
<h3>By tridion: (ASP.NET) tag</h3>
<tridion:ComponentPresentation runat="server"
PageURI="tcm:7-1535-64"
ComponentURI="tcm:7-1533"
TemplateURI="tcm:7-1532-32"/>
<h3>By tcdl: tag</h3>
<tcdl:ComponentPresentation
PageURI="##Page.ID##"
type="Dynamic"
ComponentURI="tcm:7-1533"
TemplateURI="tcm:7-1532-32"/>
<h3>by render component presentation</h3>
##RenderComponentPresentation('tcm:7-1533','tcm:7-1532-32')##
These get transformed to the following on the file system.
<h3>By tridion: (ASP.NET) tag</h3>
<tridion:ComponentPresentation runat="server"
PageURI="tcm:7-1535-64"
ComponentURI="tcm:7-1533"
TemplateURI="tcm:7-1532-32"/>
<h3>By tcdl: tag</h3>
<tridion:ComponentPresentation runat="server"
PageURI="tcm:7-1535-64"
ComponentURI="tcm:7-1533"
TemplateURI="tcm:7-1532-32"/>
<h3>by render component presentation</h3>
<tridion:ComponentPresentation runat="server"
PageURI="tcm:7-1535-64"
ComponentURI="tcm:7-1533"
TemplateURI="tcm:7-1532-32"/>
Each renders delivery-side so the user sees the correct CP in the browser.
Question(s):
Any approach gets us a DCP referenced from a PT with an agnostic format that gets retrieved presentation-side, avoiding full-site publishing. It's not really on the page so we have to be sure to publish the logo with a dynamic CT (it's html fragment in this case). Am I missing anything?
Is RenderComponentPresentation() preferred over TCDL or a specific presentation-side syntax? Any reason I'd actually want <tridion:ComponentPresentation runat="server"> instead?
Edit: I was missing the Type="Dynamic" attribute and value, also for a moment thought RenderComponentPresentation didn't create the right CP reference.
Basically you have answered your first question yourself already I think, apart from how you want to write it out in your template you have your DCP there.
My preference would be to use ##RenderComponentPresentation('tcm:7-1533','tcm:7-1532-32')## but I would actually not use it with hardcoded TCMURIs. I'd probabaly write a TBB which reads the Multimedia Component TCMURI and its Template URI somewhere from for instance a configuration Component or the Publication metadata, then your template could look something like:
##RenderComponentPresentation(${LogoUri}, ${LogoTemplateUri})##
When using Dreamwever TBBs for your layout, I would use this as a preference. If you are using another way of layout, then the next best thing would be to use the TCDL tag. Indeed to try and keep your templates code agnostic. The only time I would ever revert to writing out the tridion control code myself, is when I cannot get it there another way.

Is it possible to call the SiteEdit editable content script to output the content from TBB using RenderComponentPresentation method?

Is it possible to call the SiteEdit editable content script to output the content from TBB using RenderComponentPresentation method?
Currently, I am getting a red border line wrapped that content. When I roll over the top right corner icon, it is saying removed. However, the component content is not removed, any changes of the component template or page all have been republished and component itself is also not dynamic component.
The SiteEdit is working if I am calling the editable script directly from TBB, but if I am using RenderComponentPresentation method to call another Component Template to output that script. I only get the red border line and saying it has been removed.
Editable script: (working directly from TBB)
<!-- TemplateBeginIf cond="Content != ''" -->
<tcdl:ComponentField name="Content">${Content}</tcdl:ComponentField>
<!-- TemplateEndIf -->
Within TBB calling below method and always getting a red border RenderComponentPresentation(ComponentID, ComponentTemplateID)
Environment: Tridion 2011, SiteEdit 2009 SP3, Windows Server 2008
I guess this is not feasible with RenderComponentPresentation since what you're trying to do is nested component presentations. However you can get this by adding the siteedit markup tags directly with IsQueryBased to true
Please check the link for additional info (bottom of the section): http://sdllivecontent.sdl.com/LiveContent/content/en-US/SiteEdit_2009_SP3/concept_0D0C929C17D74A6292B2AFE0C29F4DCD
Alternative approach is to add the below markup before your nested component presentation (IsQueryBased should be true):
<!-- Start SiteEdit Component Presentation: {
"ID" : "MyCP23", "ComponentID" : "tcm:54-7894",
"ComponentTemplateID" : "tcm:54-3201-32",
"ComponentVersion" : 2,
"IsQueryBased" : true,
"SwapLabel" : "Left"
} -->
If you are rendering a Component which is not on the current Page, then it is to be expected that SiteEdit considers it removed.
You will need to mark the Component Presentation as "query based" so SiteEdit knows not to expect it on the Page. This will disable the functionality which doesn't make sense (such as drag-and-drop to move the CP around on the Page) but will otherwise work the same.
To mark it as query-based, you will need to manually output the markup (see Start SiteEdit Component Presentation command) or use some other tool to do it for you, like the functions created by Frank van Puffelen (note: I believe these only work for the 2011 version). Make sure the IsQueryBased parameter is set to true.

Can we edit Page Metadata in Tridion using SiteEdit 2009?

In SDL Tridion 2009 SP1 we have Components that are added to Structure Group and Page Metadata which are then output on the Page as a Component Presentation. We have done this to allow users to set global content per site section without the need to add it to every single Page in the Structure Group.
When we output the Component with a Component Template with manual tags we get a red box around the Presentation and the tooltip of the SiteEdit box says “Removed”. When using the exact same Component Template in the Component Presentations tab and rendering that on the page the content is editable.
Is it possible using SiteEdit 2009 to inline edit a component added as Page/Structure Group metadata?
Note: We are aware of an outstanding bug in SDL Tridion 2009 SP1 that when you go to open a component added to Page/Structure Group metadata that it always opens it read-only. Could this be causing the issue?
It is possible to do this but you have to make sure that your markup is correct. I have done this before by marking the SiteEdit Component Presentation as QueryBased which seems to make SiteEdit ignore it's usual rules where the Component must be added in the Component Presentation list
<!-- Start SiteEdit Component Presentation: {
"ID" : "CP1",
"ComponentID" : tcm:32-204298",
"ComponentTemplateID" : "tcm:32-204519-32",
"Version" : "16",
"IsQueryBased" : true}
-->
You need to either:
Expose the metadata of the page in a "visible" area of your page with the correct SE markup
Open the page in CME and edit the Metadata there.
Tridion UI 2012 added a button with "Edit Metadata" functionality, but that's not available for SE 2009
Not through inline editing. You can of course open the Page editing popup and edit the metadata there.
Of course if you want to edit fields of the Component that you've linked in Page Metadata, you can just render out that Component Presentation into the HTML and mark it with the correct SiteEdit commands as Nuno suggests.

How to access componentlink field names in third level?

How to access componentlink field names in third level?
I have a schema which contains a field as embeddable schema, in that embeddable schema, I have a component link which is derived from another schema. How can i retrieve the field of that schema?
It really depends on the purpose of those field(s), perhaps you could provide some more information?
Another option not mentioned here is to write your own C# building block to add these 3rd level components to your package. By doing so you make it a lot simpler to work with them in your DWT layout.
For example if your third-level items are used to generate a list of office locations, you could write a C# block to obtain all the relevant office location components and add them to your package as a component list 'Locations' all you would simply need to do in your DWT is loop those items and provide the display you need.
Personally I don't like to use / add functions into DWT, I try to keep it as simple as possible, again if you can provide more information about your scenario that would generate a better answer for you from the community.
As far as I know, you need a special DWT TBB to get some control over your embedded/componentlinks. Maybe you could use Nuno's Get Extensions ? http://www.sdltridionworld.com/community/extension_overview/dreamweaver_get_extension.aspx
Or use Razor Mediator, perhaps:
http://www.sdltridionworld.com/community/2011_extensions/razormediator.aspx
To prevent having to use either the DGX (as MDa suggests) or write a C# TBB (as John Winter suggests), you can also simply call RenderComponentPresentation to render the linked Component with the template that you want to use.
<!-- TemplateBeginRepeat name="Component.Fields.LinkFieldName" -->
##RenderComponentPresentation(Field, "tcm:1-23-32")##
<!-- TemplateEndRepeat -->
Where tcm:1-23-32 it the TCM URI of a Component Template that simply renders the name of the Component as a hyperlink. Inside the DWT that you use in tcm:1-23-32 you can then simply refer to all fields and properties of the linked Component like Component.Title, Component.Fields.Field1, etc.
Note that the relevant topics have been covered already in these other questions/answers:
rendering the title of a linked component - Looping through the multiple component links to get the schema name of the linked component in Dreamweaver TBB in SDL Tridion 2011
getting values of a linked component - Retrieving values of a linked component in Dreamweaver TBB - and making it SiteEditable
handling nested embedded schemas - How to handle nested repeating regions in Dreamweaver TBBs in SDL Tridion 2011 SP1
If somehow these did not help you enough, I suggest you post the XML of your Component and the DWT that you're trying to make work.
Here a quick sample using C#
Session session = engine.GetSession();
Item componentItem = package.GetByName(Package.ComponentName);
Component component = new Component(new TcmUri(componentItem.GetValue("ID")), session);
ItemFields itemFields = new ItemFields(component.Content, component.Schema);
IEnumerable<ComponentLinkField> ie = itemFields.OfType<EmbeddedSchemaField>().OfType<ComponentLinkField>();
for (IEnumerator<ComponentLinkField> e = ie.GetEnumerator(); e.MoveNext(); ) {
Component linkedComponent = e.Current.Value;
}
By using Linq you can navigate through the fields structure really quick and get results. It will give you the Component Links in the third level, of course you can use the same to go deeper in the structure.

Resources