Copied to clipboard

Flag this post as spam?

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


  • Jacob Polden 67 posts 177 karma points
    Jun 08, 2012 @ 11:53
    Jacob Polden
    0

    Inline Razor in HTML

    I am trying to get a div tag to dynamically change its visibilty by using a property in umbraco

    Something like this but in the <div> tag in the HTML

      @{
          if(Model.commentsClosed)
          {
              @:<div id="comments" style="display: none;">
          }
          else
          {
              @:<div id="comments"> 
           }
      }

    I have even tried just creating a macro for this, however it wont work due to the fact that the <div> are not balanced. VERY FRUSTRATING! 

    Any help greatly recieved!

  • Ulrich Wagner Gade Ebsen 127 posts 277 karma points
    Jun 08, 2012 @ 12:01
    Ulrich Wagner Gade Ebsen
    1

    Hi,

    You ca do it like this:

    <divid="comments" @if(Model.commenstClosed){<text>style="display:none;"</text>}">

    ....

    </div>

     

     

     

     

  • Bert 128 posts 251 karma points
    Jun 08, 2012 @ 12:10
    Bert
    0

    Create the optional class, id, style  ... then add it to the element

    <umbraco:Macro runat="server" language="cshtml"> 
    @{var optionalStyle = Model.commentsClosed ? "" : String.Format(" style=\"{0}\"", "display: none");}

    <div id="comments" @Html.Raw(optionalStyle)> ...
    </umbraco:Macro> 
  • Jacob Polden 67 posts 177 karma points
    Jun 08, 2012 @ 12:27
    Jacob Polden
    0

    Thank you for the replies, however this has to be done in the templates of umbraco. The problem I seem to be having is the inabilty to create a razor statement and pass parameters into the templates.

          <div id="comments"  style='@(Model.commentsClosed ? "display: none;" : "")'>

             <umbraco:Macro Alias="BlogPostListComments" runat="server"></umbraco:Macro>              
         <div id="respond">
                    <h3>Post a comment</h3>
                    
                    <div class="formcontainer">
                      <umbraco:Macro FormGuid="d63ecae9-5ef9-41ce-bc9b-d9445949c861" Alias="umbracoContour.RenderForm" runat="server"></umbraco:Macro>
                    </div>
             </div>
          </div>

  • Bert 128 posts 251 karma points
    Jun 08, 2012 @ 12:48
    Bert
    0

    <umbraco:Macro runat="server" language="cshtml"> 

    @{var optionalStyle = Model.commentsClosed ? "" : String.Format(" style=\"{0}\"", "display: none");}

    <div id="comments" @Html.Raw(optionalStyle)> ...

    </umbraco:Macro>

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

     

    ...

    </div> --> close comments div

  • Jacob Polden 67 posts 177 karma points
    Jun 08, 2012 @ 12:57
    Jacob Polden
    0

    When I do what you suggested, it closes the comments div tag:

     <div id="comments"/>
    <div title="Macro Tag: 'BlogPostListComments'style="border: 1px solid rgb(0, 0, 153); "/>

     

    <div id="respond">
    <h3>Post a comment</h3>
     
    <div class="formcontainer">
     
    <div title="Macro Tag: 'umbracoContour.RenderForm'style="border: 1px solid rgb(0, 0, 153); ">
    <link href="/umbraco/plugins/umbracoContour/css/defaultform.cssrel="stylesheettype="text/css"/>
     
    <div id="contourclass="comment ">
     
    <script type="text/javascript">
    </div>
    </div>
    </div>

     

  • Bert 128 posts 251 karma points
    Jun 08, 2012 @ 13:20
    Bert
    0

    strange, works for me. Maybe the problem is your macros? Try this:

      <umbraco:Macro runat="server" language="cshtml"> 
    @{var optionalStyle = Model.commentsClosed ? "" : String.Format(" style=\"{0}\"", "display: none");}
    <div id="comments" @Html.Raw(optionalStyle)> ...
    </umbraco:Macro>   
             
         <div id="respond">
         <h3>Post a comment</h3>
        form
         </div>
       </div>
  • Jacob Polden 67 posts 177 karma points
    Jun 08, 2012 @ 15:23
    Jacob Polden
    0

    It's the macro's, but they need to be there, they are the things I am trying to disable/enable. Any idea's?

  • Bert 128 posts 251 karma points
    Jun 08, 2012 @ 15:26
    Bert
    0

    try the contour macro and the other one seperate to further track down the problem.My guess is the contour one will be ok.

    If it's only one or the other, post the source here.

  • Funka! 398 posts 661 karma points
    Jun 09, 2012 @ 02:52
    Funka!
    0

    It may not be pretty, and it may throw off the syntax highlighting/coloring in the CodeMirror web editor, but this should work:

    <div id="comments" style="<umbraco:Macro runat="server" language="cshtml">
      @{ 
        if (Model.commentsClosed)
        {
          @("display:none;")
        }
        else
        {
          @("display:block;")
        }
      }
    </umbraco:Macro>">
      COMMENT STUFF IN HERE
    </div>
    

    Notice the inline macro is completely contained within the div's style attribute. Nothing unbalanced.  I use this same trick in one of my own templates!

    Best of luck to you!

  • Dave Forster 28 posts 74 karma points
    Jun 20, 2012 @ 10:34
    Dave Forster
    0

    Just out of interest, why are you using "display:none" to disable comment functionality ? Your comments could be re-enabled very easily by even a novice hacker using firebug etc.

    Wouldn't you be better off only displaying the div at all if comments are NOT closed ?

    if (!Model.commentsClosed)
Please Sign in or register to post replies

Write your reply to:

Draft