Note that there are some explanatory texts on larger screens.

plurals
  1. POMethod to encapsulate reusable view sections (i.e. tiles, portlets) with arbitrary content?
    primarykey
    data
    text
    <p>I'm still getting my feet wet with MVC and working on a site that uses a lot of "tiles" (you know, the usual "rectangular section with standardized header and some content" pattern) for the visual display. There's nothing fancy, just a way to put content into boxes for ease of layout and navigation. Currently the "tiles" (my term) are built like this:</p> <pre><code>&lt;div class="tile"&gt; &lt;div class="tile-header"&gt; &lt;h2&gt;Title&lt;/h2&gt; &lt;/div&gt; &lt;div class="tile-body"&gt; (arbitrary content) &lt;/div&gt; &lt;/div&gt; </code></pre> <p><em>Note: "arbitrary content" means any content, either static or dynamic, so not necessarily something I can plug into a variable and pass into a helper, I need a bit more flexibility.</em></p> <p>My question is, what is the optimal method to use to encapsulate this pattern and make it easier/more expressive to call from code? I know about partial views, but how would I pass arbitrary body content to a partial view? (This may simply be my limited knowledge of MVC showing) </p> <p>I tried building Razor helpers like this:</p> <pre><code>@Tile.Begin("Title") (arbitrary content) @Tile.End() </code></pre> <p>But Razor chokes because I can't include the "tile-body" closing div. (If I do then I have to pass the body content as a string variable, and that isn't always feasible)</p> <p>I could see something like this:</p> <pre><code>@using (Tile.Begin("Title")) { (arbitrary content) } </code></pre> <p>This seems the most elegant to me, though not as easy to scan as the @Tile.Begin() and @Tile.End() calls. From what I understand I would have to create a class and implement IDisposable, but when I tried returning a string of raw HTML tags from a helper class it simply wrote the encoded tag syntax to the screen, so I assume I'd run into the same issue here?</p> <p>Thanks for any advice. :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload