Escape field inside Control loop - silverstripe

New to SilverStripe and trying to decypher how things work.
I have a field in a page which I used to store some HTML code. When you view their "Holder" page, it loops over each child page and displays them all. The problem I am running into is that when I output the value it's escaping it - so I need to be able to decode it.
<% control Children %>
<h2>$Title </h2>
$ExtraHtmlBody <!-- This is escaping when outputting -->
<% end_control %>
So I tried to add a function inside my Page_Controller, but it seems that I can not call Page_Controller methods from inside the Control loop. I tried moving the function into the Page class, but it doesn't seem to have any data for $this->ExtraHtmlBody. Maybe I'm doing something wrong.

You data might already be escaped in the database itself. did you check?
Like #munomono said, if you are storing html use HTMLText or HTMLVarchar.
You can also try to disable auto-escaping in your template with $ExtraHtmlBody.RAW (at your own risks).
some info here:
http://doc.silverstripe.org/framework/en/topics/data-types#casting-html-text
http://doc.silverstripe.org/framework/en/topics/security#what-if-i-can-t-trust-my-editors
http://doc.silverstripe.org/framework/en/reference/templates#formatting-and-casting
Your controller function problem is probably just a scope issue since <% loop %> changes the scope, $Up/$Top might help. But you probably don't need that function anyway.

Related

Categorize webpages for layout and CSS purposes in Rails

I have a lot of different webpages, but most fall into 1 of 6 or 7 categories of pages. For each of these categories of webpages, I have a slightly different look and feel, like different background colors/images. I have the following in views/layout/application.html.erb:
<header class="sub<% if content_for?(:page_type) %> <%= yield(:page_type) %><% end %>">
And in every single view, I have:
<% content_for :page_type, 'information' %>
But it's really a pain to do that for every single webpage and when I want to change things around I keep having to mess with these (I have a ton of pages). So, instead I'm thinking to just use a variable:
<header class="sub<%= #page_type ? ' ' + #page_type : '' %>">
and for views:
<% #page_type = 'information' %>
The advantage is I could do a before_filter :set_page_type in the controller and set the page type once for all the views it controls (a big help).
Maybe the best thing for me to do is just use the first folder of the URL as the category of the webpage. I'll have to restructure the URLs, but that might make sense to do so anyway. I do have some top-level pages that would have to remain so.
This has to be a fairly common situation, what is the best way to categorize most pages and use that categorization in layouts?
I’m usually using a helper for the body class — something like this:
def body_class(c = nil)
#body_class ||= [controller.controller_name]
#body_class << c
#body_class.flatten.compact.uniq.join(" ")
end
This will by default include the controller name (you could also include the action etc..
Then I call this helper in views as needed, e.g.
<% body_class "bar" %>
Since the helper always returns a class string, you can then call the same helper in you layout (I use the body tag), likely without arguments:
<body class="<%= body_class %>">
Which would render in the previous example for a controller called FoosController the following:
<body class="foos bar">
If you define the helper in e.g. the application controller and make it available to the views using helper_method, this should hopefully much do what you’re after.

How to get "Title" attribute from the "Page" object

I use SilverStripe as the CMS and I'm stuck now and don't know how to access "Title" attribute from the "Page" object.
I tried:
$Event.Trainer.Title
But it doesn't work. The "Trener" is the "TrenerPage" object. How can I access to Trener->Title attribute?
You can't traverse three levels in SilverStripe templates (at least in version 2.x). Two is the maximum.
What you need is something like this:
<% control Event %>
$Trainer.Title
<% end_control %>
Your question seems to inconsistently switch between "Trainer" and "Trener", I'm guessing one of those is a typo?
If the template is for the page you wish to display the title of, all you need to use is $Title in your template and it will output the title of the rendering page.
If the template is NOT for the page you wish to display the title of, then like xeraa said, you should use a control block.
The title is directly within the Page object.
Just using $Title should do the trick. To help you with all the methods available in the Page object go to:
http://doc.silverstripe.org/sapphire/en/reference/built-in-page-controls
Since the question is not very clear, I'll take a shot at another answer.
If you derived the Trainer_Page from the Page object it still inherit the $Title attribute directly. Unless you overider the $Title attribute yourself in the Trainer_Page object, PHP will default it back to the parent class. In that case just use $Title.
Beware of the case as $title and $Title are not the same.
Good luck.

Dynamically generated HTML in C# to be formatted

I have an ASP.NET web forms site with a rather large menu. The HTML for the menu is dynamically generated via a method in the C# as a string. I.e., what is being returned is something like this:
<ul><li><a href='default.aspx?param=1&anotherparam=2'>LINK</a></li></ul>
Except it is a lot bigger, and the lists are nested up to 4 deep.
This is written to the page via a code block.
However, instead of returning a flat string from the method I would like to return it as formatted HTML, so when rendered it looks like this:
<ul>
<li>
<a href='default.aspx?param=1&anotherparam=2'>LINK</a>
</li>
</ul>
I thought about loading the html into an XmlDocument but it doesn't like the & character found in the query strings (in the href attribute values).
The main reason for doing this is so I can more easily debug the generated HTML during development.
Anyone have any ideas?
Maybe you can work with an HtmlTextWriter? It has Indenting capabilities and it may actually be a cleaner thing as you could write straight into the output stream, which should be more "in the flow" than generating a string in memory etc.
Is there a reason you want to do this? This implicitly minified HTML will perform slightly better anyway. If you do still need to render the HTML for pretty display, you will either need to incorporate indentation into the logic that generates the output HTML or build your content using ASP.NET controls and then call Render().
Try loading the HTML into the HTML Agilty Pack. It is an HTML parser that can deal with HTML fragments (and will be fine with & in URLs).
I am not sure if it can output pretty printed (what you call "formatted") HTML, but that would be my first approach.
I like to use format strings for this sort of thing, your HTML output would be generated with;
String.Format("<ul>{0}\t<li>{0}\t\t<a href='{2}'>{3}</a>{0}\t</li>{0}</ul>",
System.Environment.NewLine,
myHrefVariable,
myLinkText);

Formatting strings in ASP.NET Razor

I am currently writing a small templating system in ASP.NET to allow users to add content. For example, the user can enter the string (variable type is string).
topHeader[x] = "They think it's all over. It is now!";
However, one change that's needed is the ability to add some basic HTML tags within this content, so the following can be done
topHeader[x] = "They think it's all over. <strong>It is now!</strong>";
or
topHeader[x] = "They think it's all over. <a title="Football News" href="URL">It is now!</a>";
If you add such things into strings now they are not formatted as HTML, but I want to somehow escape them so that they can be. Naturally I've looked on the Internet for the answer, but as Razor is fairly new there's not much out there to help me out.
Anyone have an idea of how to do this?
You need to create an IHtmlString implementation holding your HTML source.
Razor plans to have a helper method to do this for you, but, AFAIK, it doesn't yet, so I believe you'll need to create your own class that implements the interface and returns your HTML from the GetHtmlString() method.
EDIT: You can use the HtmlString class.
You can either change your topHeader dictionary to hold IHtmlStrings instead of Strings, or you can leave your code as is, but wrap it in an HtmlString in the Razor view:
<tag>#new HtmlString(topHeader[x])</tag>
Make sure to correctly escape any non-HTML special characters.
The helper method they added is called Html.Raw() and it is much cleaner.
Here is an example:
#Html.Raw("Hello <a>World</a>!")
SLaks is right, but you don't need to write your own implementation of IHtmlString, there's one built in to System.Web called HtmlString. So:
topHeader[x] = new HtmlString("They think it's all over. <a title=\"Football News\" href=\"URL\">It is now!</a>");
Should do the trick.

asp.net MVC action link need to have the registered on the actual word

got this actionlink:
<%= Html.ActionLink("Corian® Worktops", "Index", "Corian")%>
the word corian has to carry the registered symbol or the word can not be used, but it seems to process, i know i could just write this as a normal href but it kinda defeats the object if there is another solution.
has any tried and successfully caried something like this out?
thanks
It works normally
<%= Html.ActionLink("RegistededMark®", "Action")%>
Use the normal ® symbol but make sure the font in HTML displays it correctly.
I do not know why but having static text in the views gives me the chills. I would rather suggest that you use a resource provider to fill in your link text. That way you will not be bothered by the html encoding stuff.

Resources