Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Robert Valcourt 70 posts 103 karma points
    Apr 06, 2011 @ 11:39
    Robert Valcourt
    0

    Asking for help for possible inline razor

    I've heard that razor scripting can be used in templates. I'm looking for some assistance to create the following functionality.

    In one of my templates, I have a macro. I would like this macro to be called only under certain conditions. The condition is a True/False property in one of my Content nodes. If this can be made to work, it would go something like this (not real code):

    <% if ContentNodeAlias/TruefalsePropertyAlias = 1 or (true) {

    <umbraco:Macro Alias="siteFunction" runat="server"></umbraco:Macro>

    }

    %>

    So basically, the script looks for a specific node and the value of a true/false property, and if checked, will render a macro, otherwise not. Is this possible?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Apr 06, 2011 @ 11:44
    Sebastiaan Janssen
    0

    No you can't do it like that as razor can't render macro's (easily).

    Wouldn't it be much much easier to just put the logic IN the macro (where it belongs, more or less). So just say in your XSLT/Razor file "if this property is true then proceed with whatever, else just not return anything"?

  • Robert Valcourt 70 posts 103 karma points
    Apr 06, 2011 @ 11:47
    Robert Valcourt
    0

    If the macro was calling an XSLT file where I could put that conditional statement in, I would do that for sure. In my case, the macro is calling a usercontrol and I really dont have the ability or knowledge to make it conditional as described.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Apr 06, 2011 @ 11:47
    Sebastiaan Janssen
    0

    FYI: Inline Razor requires a macro control, and you can't nest macro's, this will throw an error (The 'Text' property of 'umbraco:Macro' does not allow child objects):

    <umbraco:Macro Language="cshtml" runat="server">
    @if (Model.MyProperty == False)  { //This line is inline Razor, it needs to be inside an umbraco:Macro control
      <umbraco:Macro Alias="siteFunction" runat="server"></umbraco:Macro>
    }
    </umbraco:Macro>

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Apr 06, 2011 @ 11:50
    Sebastiaan Janssen
    0

    Ah in that case, in your macro, you can create macro parameters ("Developer"> "Macros"> "Your Macro"> go to the parameters tab). You can fill the macro parameter with this syntax:

    <umbraco:Macro Alias="siteFunction" runat="server" MyParameterAlias="[#theTrueFalsePropertyAlias]"></umbraco:Macro>
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Apr 06, 2011 @ 11:51
  • Robert Valcourt 70 posts 103 karma points
    Apr 06, 2011 @ 12:14
    Robert Valcourt
    0

    Thank you, i'll have a look.

  • Alex 78 posts 136 karma points
    Apr 06, 2011 @ 12:50
    Alex
    0

    According to this blog you can use RenderPage but I haven't tested it myself

    http://joeriks.wordpress.com/2011/03/11/better-structure-for-your-razor-scripts-with-renderpage-in-umbraco/

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Apr 06, 2011 @ 12:52
    Sebastiaan Janssen
    0

    That works great but not if you want to render a usercontrol. :-) It can only render Razor webpages (and possibly other pages?) but not usercontrols individually.

  • Alex 78 posts 136 karma points
    Apr 06, 2011 @ 12:54
    Alex
    0

    Ahh yes, of course. I am so involved in soaking up the Razor implementations I have completely disregarded all other forms of macros! :-)

Please Sign in or register to post replies

Write your reply to:

Draft