Macro Expression not working in Template Layout

Cameron Asbury asked on January 22, 2020 19:24

Hello,

I am attempting to place a macro expression in my template layout. I click insert macro and find the CurrentDocument.DocumentName macro and insert it. After inserting however it only displays the macro, my username, and a hashcode where I place it ({% CurrentDocument.DocumentName | user blah blah hash blah blah). Is it possible to place macro expressions here or am I missing something obvious?

Thanks!

Cameron

Correct Answer

Brenden Kehren answered on January 22, 2020 20:31

Assuming your template layout is set to Text/HTML it should work fine. Otherwise if it's an ASCX template type, it won't work.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Cameron Asbury answered on January 22, 2020 21:26

That's the issue! I'm using an ASCX template type, I'll see if I can swap to the other.

0 votesVote for this answer Mark as a Correct answer

Cameron Asbury answered on January 22, 2020 21:27

Brenden do you have a link to the upside/downsides of using one template type vs the other I could read?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 22, 2020 21:41

No documentation really on that, it's a preference. Some differences though:

ASCX allows you to use C# code in it, Text/HTML does not. Text/HTML allows you to use the K# Macro syntax in it, ASCX does not.

Some things are easier to write macros for while some things are easier to use straight C# for.

1 votesVote for this answer Mark as a Correct answer

Dat Nguyen answered on January 22, 2020 21:57

If you don't need to write C#, you could take advantage of the fact that HTML layouts do not require compilation, which means faster initial load and you can modify the layout on precompiled websites.

1 votesVote for this answer Mark as a Correct answer

Cameron Asbury answered on January 22, 2020 21:59

Can I input c# directly into the template on the front end or do I need to write code in the backend and add it to the project for that?

Cameron

0 votesVote for this answer Mark as a Correct answer

Dat Nguyen answered on January 22, 2020 22:12

Yeah, if you edit the layout with the Portal engine, you can always put C# code in the layout like this:

<% CMS.DocumentEngine.TreeNode node= DocumentHelper.GetDocuments(...); %>
1 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on January 23, 2020 08:54

@Dat - in some cases the code blocks <%...%> may work, or even the displaying expression <%=...%> like
<%= CMS.MacroEngine.MacroResolver.Resolve("{%CurrentDocument.DocumentName#%}") %> might work, but it depends on other controls/web parts on the page and the life cycle. So, in most of the cases you may hit this error. So, officialy the code block and inline expressions are not supported in Kentico, in the ASCX layout.

0 votesVote for this answer Mark as a Correct answer

Dat Nguyen answered on January 23, 2020 10:04

@Juraj - I've developed Portal engine sites using C# code and the Kentico API in ascx layouts extensively, and I've never experienced that error. (Kentico 10 and beyond)

Also, the documentation says this: ASCX layout type - This type of layout code supports both HTML and ASP.NET markup, i.e. the same syntax that you would use to edit a standard web form or user control, including inline code and embedded controls.

0 votesVote for this answer Mark as a Correct answer

Juraj Ondrus answered on January 23, 2020 10:40

Well, may it was changed in later versions but I have seen issues on Kentico 10, 11 and 12 with this. So, maybe you are just lucky and are using it always in the same way. What I wanted to say is that it may not work in all cases and you need to know when are those inline code called and what is the page and control life cycle - so some data may not be accessible at given time. So, I would rather recommend using code behind as it is clearer, easier to debug and maintain.

0 votesVote for this answer Mark as a Correct answer

Dat Nguyen answered on January 23, 2020 10:51

I definitely think that business logic and presentation should be separate, and I don't particularly like putting C# code in the layouts. I've had clients that wanted all of the site to be implemented in Portal engine, so I know that it can be done. But like you, I also would not recommend it. Just answering Cameron's question about whether it can be done.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.