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

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.

Related

Escape field inside Control loop

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.

How do you access the title (Node module element) field in Drupal 7?

I have a computed field in a Drupal 7 content type that is populated by my description (text) field:
$entity_field[0]['value'] = $entity->field_desciption['und'][0]['value'];
It works fine. I want to create another computed field that is populated by the title (Node module element) field.
I tried with the following lines, but they don't work.
$entity_field[0]['value'] = $entity->title['und'][0]['value'];
$entity_field[0]['value'] = $node->title;
How can I achieve this?
The node title is not a field; therefore, using $entity->title['und'][0]['value'] will not work. What you should use is $entity->title.
As side note, to get the value of a field, you should use field_get_items(), which takes care of the language set for the field, which isn't necessarily LANGUAGE_NONE.
If it's a node module element, it should be accessible via $entity->title directly.
Try a print_r($entity); die; to get all elements of the entity. Hope this help you.
You should look at printing the array/object to the page to see what you are working with exactly.
Try adding print_r($entity); or print_r($node); to the page where the entity or node is displayed followed by exit;
You can then right click the page and click 'View page source' to display the output in a structured format. Use this to see the variable names, object/array types and hierachy to then write your full variable code correctly.
print_r($node);
exit;
I would imagine it should have been $node->title though...

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.

Setting Page Title from an HtmlHelper class

I have an HTML Helper that essentially renders static content read from HTML files (inside of a vdir). There are cases when the HTML file has a title element defined and in such cases, the current page should use the given title. The content inside of the body should be rendered where the Helper class is referenced in the View.
This is how I call the helper.
<%=Html.StaticContent("staticcontent.htm",
new List<StaticContentTag>()
{
new StaticContentTag()
{TagKey=ReplaceTags.MarketName,
TagValue = "Austin"}
}, Model, true) %>
I'm passing in the ViewModel so that I can set the title and the last parameter is a flag that says whether to force the title or not.
The head has the title defined like this.
<title><%=Model.Title%></title>
I know what I'm doing wrong here by referencing the Model.Title element before even calling the helper. Any ideas on how I can work around this?
i believe ur title tag is rendered before u call the html helper in ur view. the purpose of helpers is to render html tags where they are called not to change contents of already rendered tags that can be done through javascript. however i would not use all that new keywords in my view. rather i would make a view model containing all required information for the view and then i would have no problem writing statement
<title><%=Model.title%></title>

mvc DataAnnotations how to make field no editable in 3.5

I have a few field in my entity that i wish to be non-editable. Looking in the docs it seems like "EditableAttribute" would do the trick. However this is only 4.0
Just wondering if there are other attributes that would have the desire effect. So be clear,
i have a field called "DateRegistered" i wish to display this as string not text field using "Html.EditorFor"
The [ReadOnly] attribute should work in 3.5.
Why are you using an editor template for something that should be read only? Display templates and the <%= Html.DisplayFor(x => x.DateRegistered) %> method seem more appropriate in this case.

Resources