Copied to clipboard

Flag this post as spam?

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


  • Garrett Fisher 341 posts 496 karma points
    Oct 12, 2016 @ 14:58
    Garrett Fisher
    0

    In-template razor logic to run macro if certain field has certain value

    Hi,

    I have a field on a document type that is true or false and specifies whether the admin wants to show something. That "something" is written in an XSLT macro/file. And my umbraco.library:RenderMacroContent() is not working within that XSLT file so I wan to try another way -- I was thinking perhaps right within the masterpage template checking this field's value and, if true, then write the macro call. How can I do this? Don't I need a script tag or something around the razor/cshtml logic?

    If(showStuff=1) {
      <umbraco:Macro Alias="ThingIWantToShow" runat="server"></umbraco:Macro>
    }
    

    Is basically what I want but I need the correct syntax and form.

    Thanks, Garrett

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Oct 12, 2016 @ 21:07
    Alex Skrypnyk
    0

    Hi Garrett,

    If you want to add if statement to the masterpage use this syntax:

    <% if (true) { %>
    <umbraco:Macro Alias="ThingIWantToShow" runat="server"></umbraco:Macro>
    <% }%>
    

    Hope it will help.

    Thanks,

    Alex

  • Garrett Fisher 341 posts 496 karma points
    Oct 12, 2016 @ 21:17
    Garrett Fisher
    0

    Thanks so much for trying to help, Alex! I will pop that in right after I get one more reply from you -- I also need to know how syntactically to say, specifically, "if useGigya is true". Is it:

    <% if (Model.Content.useGigya = 1) { %>
    <umbraco:Macro Alias="ThingIWantToShow" runat="server"></umbraco:Macro>
    <% }%>
    

    I'm sorry but I am all about XSLT and am just now learning Razor and CSHTML. The more complete and exact the answer, the better :)

    Thanks again,

    Garrett

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Oct 12, 2016 @ 21:34
    Alex Skrypnyk
    1

    Garrett, try

    <% if (Model.Content.useGigya == 1) { %>
    

    or

    <% if (Model.Content.useGigya) { %>
    

    Depends on what type is "useGigya" field.

    Thanks,

    Alex

  • Garrett Fisher 341 posts 496 karma points
    Oct 13, 2016 @ 14:52
    Garrett Fisher
    0

    That field is a true/false checkbox. With either of those code snippets around the macro call I get this error when loading the page from the front end:

    Compiler Error Message: CS0103: The name 'Model' does not exist in the current context.
    

    Here is the complete snippet from the master masterpage template.

    <% if (Model.Content.useGigya == 1) { %>
           <umbraco:macro Alias="GigyaSettings" runat="server"></umbraco:macro>
    <% } %>
    

    Am I missing something? Do I need to import something before it to tell the template what Model is?

    Thanks again,

    Garrett

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Oct 13, 2016 @ 15:53
    Alex Skrypnyk
    1

    Hi Garrett,

    Try this code:

        <% if (Node.GetCurrent().GetProperty("useGigya") != null && Node.GetCurrent().GetProperty("useGigya").Value.Equals("1"))
            { %>
           <umbraco:macro Alias="GigyaSettings" runat="server"></umbraco:macro>
    <% } %>
    

    Thanks,

    Alex

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Oct 13, 2016 @ 15:57
    Alex Skrypnyk
    0

    By the way it's not Razor :) It's webforms syntax.

  • Garrett Fisher 341 posts 496 karma points
    Oct 13, 2016 @ 15:58
    Garrett Fisher
    0

    Error:

    Compiler Error Message: CS0103: The name 'Node' does not exist in the current context
    

    What am I missing? Thank you so much for your replies, Alex.

    //Garrett

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Oct 13, 2016 @ 15:59
    Alex Skrypnyk
    100

    Add this line to the top of document -

    <%@ Import Namespace="umbraco.NodeFactory" %>
    
  • Garrett Fisher 341 posts 496 karma points
    Oct 13, 2016 @ 16:06
    Garrett Fisher
    0

    BOOM. Thank you again, so much, Alex. I can now move forward with that Gigya implementation stuff, thanks to you.

    Cheers,

    Garrett

  • Alex Skrypnyk 6133 posts 23952 karma points MVP 7x admin c-trib
    Oct 13, 2016 @ 16:07
    Alex Skrypnyk
    0

    Glad to help you.

    So this topic is solved, can you mark it solved?

    Have a nice day, write more topics!

    Thanks,

    Alex

  • Garrett Fisher 341 posts 496 karma points
    Oct 19, 2016 @ 17:45
    Garrett Fisher
    0

    I don't know how to mark it as solved. Sorry!

    //Garrett

Please Sign in or register to post replies

Write your reply to:

Draft