RenderComponentPresentation before any other markup Tridion Razor Page - templates

I have a page template in Tridion 2011 with Razor code that prints information based on RenderComponentPresentation() as the first thing in the page. No other markup comes before it, because the component, not the page, contains the initial markup. Unless I put at least one character before the first RenderComponentPresentation in the published output, the template refuses to render any presentations.
So, for example, if this is all that is in the layout TBB this works (in my real code the tcms are real of course):
<
#RenderComponentPresentation("tcm:mytcm","tcm:myothertcm")
but this does not
#RenderComponentPresentation("tcm:mytcm","tcm:myothertcm")
The first prints the contents of the component preceded by the "<", whereas the second does nothing at all. I don't want to have ANY markup directly at the start of the page template, I want the first thing to be the component. Is it possible?

I've just done a quick test in Template Builder using the latest version of the Razor Mediator (1.2) and couldn't replicate your issue.
Maybe you could try:
<text></text>
#RenderComponentPresentation("tcm:mytcm","tcm"myothertcm")
It won't render any additional markup but may trick the mediator into doing what you want (though like I said, I can't replicate your problem so can't verify whether it does).

Normally with Razor you iterate over any and all Component Presentations on the page, and right now I'm working with
#foreach(var cp in ComponentPresentations){
#cp.RenderComponentPresentation()
}
This will render every component on the page, regardless of predefined schema's or templates. Your issue however suggest a problem elsewhere. What kind of output does your page template generate (do mind its the page template using a compound template which in turn includes the Razor TBB you describe here). Is it .aspx, HTML or other? And what is the Component templates' output? is it an HTML fragment, or anything else?
As far as you syntax goes, that should be just fine other than the template invocation:
#RenderComponentPresentation("tcm:x-xxx-xx", "tcm:xx-xxx-xx")
I have a feeling this code only works when used within HTML tags, though, but that's just a hunch.

Bit of a hack but have you tried:
<text>#RenderComponentPresentation("tcm:x-xxx-xx", "tcm:xx-xxx-xx")</text>
or
#Html.Raw(RenderComponentPresentation("tcm:x-xxx-xx", "tcm:xx-xxx-xx"))
Disclaimer: not really used Razor mediator. Just Razor.

Related

Drupal 8 Webform: how to display text input on one page on the next page?

I am trying to develop a multistep webform in Drupal 8 using Webform 8.x-5.1. I have written a WebformHandler that extends Drupal\webform\Plugin\WebformHandlerBase and made it available to the webform.
In the first step of the webform, I collect a text-field. I would like to display the value of that text-field in an HTML element (Advanced HTML/Text or Basic HTML) on the second page after doing some computation.
I have overwritten submitForm() in the WebformHandler and in it assign the value I want to the HTML element as follows:
$form['elements']['page_name']
['advanced_html_element']['#text'] = '...my HTML...';
Using ksm() I can see that this assignment works, but the the HTML element is not rendered with my HTML: the element is either invisible or contains the initial value set up in the form editor.
Clearly I'm missing something. Should I be using something other than submitForm? Can anyone help me?
It's been a long haul, but I've finally worked out how to do what I want to. The following works for me.
Firstly, I discovered the method validateForm in WebformHandlerBase. On each page in a form with multiple pages, you will find that the following methods are called in the order given here:
submitForm (called once)
alterForm(called possibly more than once)
validateForm (called once)
The name validateForm leads me to believe I may be misusing this method, but that is where I set up the elements on the following page that I wish to programmatically initialise. It works, so what the hey!
In validateForm, I initialise the elements that appear on the following page as follows:
$form_state->setValue(<element name>, <data structure>);
The <element name> is the name you give the element in the form editor ("Build" tab). The <data structure> has to be correct, of course: I suggest you find the appropriate structure by first filling in the element on the next page manually and seeing what turns up in $form_state.
There is also a $form_state->getValue(<element name>), which seems to me to mean that $form_state can also be used for storing session data, say in hidden fields. I initially used Drupal::service('tempstore.private')->get('xxx') for storing data that had to be available across page boundaries, but $form_state might be a cleaner solution.
I hope this helps someone: I spent a horribly long time trying to get this to work.

django - ckeditor bug renders text/string in raw html format

am using django ckeditor. Any text/content entered into its editor renders raw html output on the webpage.
for ex: this is rendered output of ckeditor field (RichTextField) on a webpage;
<p><span style="color:rgb(0, 0, 0)">this is a test file ’s forces durin</span><span style="color:rgb(0, 0, 0)">galla’s good test is one that fails Thereafter, never to fail in real environment. </span></p>
I have been looking for a solution for a long time now but unable to find one :( There are some questions which are similar but none of those have been able to help. It will be helpful if any changes suggested are provided with the exact location where it needs to be changed. Needless to say I am a newbie.
Thanks
You need to mark the relevant variable that contains the html snippet in your template as safe
Obviously you should be sure, that the text comes from trusted users and is safe, because with the safe filter you are disabling a security feature (autoescaping) that Django applies per default.
If your ckeditor is part of a comment form and your mark the entered text as safe, anybody with access to the form could inject Javascipt and other (potentially nasty) stuff in your page.
The whole story is explained pretty well in the official docs: https://docs.djangoproject.com/en/dev/topics/templates/#automatic-html-escaping

woocommerce advanced templating

i´m developing a theme and for some reason i need to move the default position for breadcrubms (also for many other things) over woocommerce themes. Then i realised to do something like this on my functions.php:
function woocommerce_remove_breadcrumb(){
remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);
}
add_action('woocommerce_before_main_content', 'woocommerce_remove_breadcrumb');
function woocommerce_custom_breadcrumb(){
woocommerce_breadcrumb();
}
add_action( 'woo_custom_breadcrumb', 'woocommerce_custom_breadcrumb' );
And then on any template file, output the breadcrumb just with:
<? do_action('woo_custom_breadcrumb'); ?>
And works. My question is more than that. Is this the correct approach for something like this? I mean for anything over woocommerce, not just breadcrumb, for any pice, ratings, titles, buttons, sidebar, and so on.
What i´m thinking on is why woocommerce templates don´t come with more deep code. I mean, why there´s no such a single-content-loop.php template where you can just change the order of things, title, category, content, images, etc. in an easy way rather that hooking into functions?
I think that is an acceptable way to call the breadcrumbs explicitly. Sometimes it is easier to call a specific function than remove everything around it!
As for changing the order of things and getting into advanced customization; there isn't a single file, but a number of files working together. Create a folder in your themes root called 'woocommerce' and copy the following files for a safe override:
woocommerce/woocommerce-hooks.php:
Here are your hooks, including the ones you are overriding in your themes functions.php. Here is where you can experiment with removing and repositioning certain elements on your product page. Search for 'Sidebar' and you will see where the 'woocommerce_sidebar' action is added with the function it references in...
woocommerce/woocommerce-template.php:
Here are the functions used in template files to output content based on conditional statements. For instance, search for the 'Single Product' series and you can see which template files are used for which functions. For instance 'woocommerce_template_single_title' uses 'single-product/title.php' - if you copy over this folder and file you can make very specific edits to just the title section
Between these two files and their accompanying references (like title.php) I believe you can do the things you described. Let me know how it works out! I'm new to woocommerce too!

Whitelist tags exempt from escaping using Go's html/template

Pass a []byte into a template as the body of a message post on a forum-style web app. In the template, call a method to convert to string and along the way, switch out all newlines for line breaks:
<p>{{.BodyString}}</p>
...
func (p *Post) BodyString() string {
nl := regexp.MustCompile(`\n`)
return nl.ReplaceAllString(string(p.Body), `<br>`)
}
What you'll end up with:
paragraphs <br> <br>in <br> <br>this <br> <br>post
I don't want to pass the entire post in with HTML(p.Body), as it represents third party data from potentially untrustworthy sources. Is there a way to whitelist only some tags for formatting purposes using the vanilla Go1 template package?
I do think you want to parse the HTML. The HTML parser in exp/html was deemed incomplete and so removed from Go 1, although the exp tree is still in the Go source tree and can be accessed by weekly tag, for example. I don't know exactly what is incomplete. I used it for a simple task once and it met my needs.
Also of course, check the dashboard and see related SO post, Any smart method to get exp/html back after Go1?, mostly for the recomendation of http://code.google.com/p/go-html-transform/
I'm affraid the template package cannot help with this too much. If you want to remove specific (black-listed) tags (resp. the sub-tree enclosed by such tags) or allow to pass only specific tags (white-listed) then I think probably nothing less than parsing and rewriting the html AST can be a good solution. That said, one can see here and there some crazy REs trying to do the same, but I don't consider that a "good solution" and I doubt they can be a "correct" solution in the general case of a specs conforming HTML, including several legal irregularities, as it is probably ruled out of a regular grammar category problem.

understand the purpose of jsf ui:composition

What is the usefulness of the following?
<ui:composition template="template.xhtml">;
"In a template client page using <ui:composition>, anything outside of the bounds of a tag is ignored and is not included in the rendered output" (JavaServerFaces 2.0, the complete reference, pg.61)
Since everything outside <ui:define> is ignored, why put anything there? Nothing has to be put outside the <ui:define>.
But doing so, all I get will be the template itself with only some "variable" parts filled.
It seems not to be this big deal.
Another thing I don't understand, is that template attribute of composition element is optional. What does it represent a template client without the reference to a template?
What is the usefulness of the following?
<ui:composition template="template.xhtml">
With this you can declare to use a basic template which has placeholders to insert template definitions. This is more useful than doing it the other way round. You would need to include for example the header, footer and/or menu in every page again and again. With a template you don't need to do this. It just goes in the template.
"In a template client page using <ui:composition>, anything outside of the bounds of a tag is ignored and is not included in the rendered output" (JavaServerFaces 2.0, the complete reference, pg.61)
Since everything outside is ignored, why put anything there? Nothing has to be put outside the <ui:define>.
You don't need to do so. Why would you? Okay maybe the basic tutorial does that, but that's just for demonstration purposes. "This will not be included in the rendered output" and so on. On the other hand, if you happen to use a visual editor, then the content outside <ui:composition> will be regarded. See also Is there a way to run a JSF page without building the whole project?
Another thing I don't understand, is that template attribute of composition element is optional. What does it rapresent a template client without the reference to a template?
A simple include file which you can include by <ui:include>.
See also:
How to include another XHTML in XHTML using JSF 2.0 Facelets?