Resolve a Kentico localisation macro in a transformation? - macros

As the title suggests I am trying to resolve a localisation string inside a repeater. I have a wysiwyg editor to input some html on the form tab of the document type, so the source would look like this
Field1: "{$localstring$}"
Then in the transformation I have
<li><%# Eval("Field1") %></li>
This outputs the string as
{$localstring$}
and doesn't resolve this as a macro and go lookup the localstring in the UI culture localisation.
I have tried different things including
<%# Eval(CMS.GlobalHelper.ResHelper.LocalizeString("Field1")) %>
and
<%# Eval(CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros("Field1")) %>
all of which give the same output, can anyone point me in the right direction? I am sure it's the way Eval is being called.
Thanks in advance.

in case somebody else searches for this: if you want to use localization string custom.my-string in ASPX transformation, you should resolve it as follows:
<%# CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros("{$custom.my-string$}") %>
note: no spaces! if you add spaces like this: "{$ custom.my-string $}" - it WILL NOT work.

The correct syntax is following:
<%# CMS.CMSHelper.CMSContext.CurrentResolver.ResolveMacros(Eval("Field1").ToString()) %>

Related

EJS includes not working

I have an EJS file called 'test.ejs', which contains:
<p>This is a test statement</p>
Then, I have another file called 'index.ejs' which is in the same folder as the other EJS file. Index.ejs contains:
<%- include ("test") %>
<p>Hello world</p>
However, when I load the page, I do not get any output. Instead, the page just loads endlessly.
My routes in Express are being handled properly and my controllers are working too. All this is tested.
Also, can someone refer me to good EJS documentation?
Try this:
<% include ./test %>
<p>Hello world</p>
try: <% include /pathname/test %>
Documentation is sparse but the official one works
http://www.embeddedjs.com/
or
http://ejs.co/

Use an imported class without specifying the package in JSP

In my JSP I have
<%# page import "com.example.Elenco" %>
but inside a very small scriptlet, whenever i try to instantiate Elenco, it requires the full name.
If i write
Elenco a = new Elenco(blabla);
Eclipse gives the following error: "Elenco cannot be resolved to a type", and I have to write
com.example.Elenco a = new com.example.Elenco(blabla);
that is pretty uncomfortable. Do exist any method to make the package name not mandatory?
Your import directive seems to be wrong. Try
<%# page import="com.example.Elenco" %>
You are missing the =.
The syntax is
<%# page import="com.example.Elenco" %>
^--- you need an equal sign here

How exactly does Rails prefills forms?

I have a (simple) question for my own curiosity:
I'd like to find out how Rails prefill forms with posted values like... you know, when there's a validation error on some models' attributes then you do something like "render :edit" and the form is magically prefilled.
What exactly are the mechanisms that Rails use to do such a thing? I didn't manage to find any documentation on this subject and I'd like to understand the magic.
So if someone can give me some explanations on this subject, I'll be glad to read that!
Thanks!
[Edit] And a subsidiary question: when a model inherits from another (STI) do we have to do something in particular to prefill forms?
You are mostly using the form_for helper in this style:
<%= form_for #person do |f| %>
<!-- Some more stuff here -->
<%= f.text_field :first_name %><br />
<!-- Some more stuff here -->
<% end %>
What this essentiall does is, it generates a text field that is filled with the value of #person.first_name.to_s. When an error happens, #person.first_name is filled with the errornous value. If you create a person (#person = Person.new), then #person.first_name.to_s is "".
So rails just fills the text field with the value, the attribute has.
f by the way is a rails FormBuilder. It's methods are documented here, if you want to take a closer look at the source.

Asp.net Mvc Display template of String, but now every simple type wants to use it!

I created a Display Template which when passed a string renders a disabled text box
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<String>" %>
<%: Html.TextBoxFor(model => model, new { disabled = "disabled" })%>
Which works great. However, for some reason MVC wants to try and stuff DateTimes and Ints through it as well, which is throwing exceptions
The model item passed into the dictionary is of type 'System.Int32', but this dictionary requires a model item of type 'System.String'.
Any ideas?
You don't need to strongly type the template to a String.
you can try something like this :
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<%= Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
, new { disabled = "disabled" }) %>
And in your view you call it like this
Html.DisplayModelFor(model => mode.name);
For more information see an example of the default built-in editor template for the string in Brad Wilson article in his his series on Templates in ASP.NET MVC.
You should consider going through the complete series. I can't express how helpful this series was for me.

MVC2 Custom HTML Helper and <%: %> Syntax

Is there any way to use a custom html helper with the <%: %> syntax ?
I know that if i'm use the code below, it's ok, but it's seems not so elegant and secure.
<%= Html.MyHelper("Some Data")%>
I mean, use <%= %> is the best practices?
Have your helper return an MvcHtmlString instead of a string. Also, please use <%: as much as possible.
HTML helpers create HTML, which is normally expected to be output raw with <%= %>. If you used <%: %> to HTML-escape the output of an HTML helper, you'll see the HTML source it produced on the page as text (eg literally <input name="foo" value="bar"> on-screen), which is probably not what you want.
It is up to the helper to HTML-escape any text content inside them, for safety. Yes, if you write a custom HTML helper and get it wrong—forgetting to HTML-encode strings your helper is putting in text content or attribute values in the output—you'll have security holes. You need to know what you're doing with escaping to write an HTML helper.
Microsoft, unfortunately, apparently don't, as the very first example in their tutorial completely fails:
return String.Format("<label for='{0}'>{1}</label>", target, text);
Whoops. Hope those ID and text strings didn't come from untrusted data!
[why are web tutorials always so lamentably terrible at escaping issues?]