Advanced Orchard Theming - Object Model vs Placement.info? - orchardcms

I have been tasked with converting a design heavy, fairly advanced HTML template for a site into an Orchard theme and I am struggling with the best way to accomplish certain things. The theme is built on bootstrap and is a modern responsive HTML template like you might find on ThemeForest or something. The site will have a number of content types (staff members, portfolio items, partners, etc.) and will need a number of templates. The content types will have a large number of fields (upwards of a dozen) inside of custom content parts.
Based on what I have read the proper way to do theming in Orchard is using placement.info in combination with alternates, wrappers, etc. This gracefully handles if parts or properties are added/removed. However, this technique is quickly becoming overwhelming, since I have to declare the name and order of every field/part in the placement.info for every content type, and every display type of that content type. Each field of each content type then needs to be wrapped in very specific html. This creates an issue because a single page can be split out into potentially a couple dozen views, with HTML tags opening in one view and closing in another.
The best work around for this I have found is to basically ignore the placement.info file and build templates just by traversing the object model. So basically, for a portfolio page, I would copy in the template HTML I have and then replace the text values with values from the model. This might look something like:
<li class="#Display(Model.ContentItem.PortfolioPart.PortfolioCore.Value.ToLower())">
<a href="#Url.ItemDisplayUrl(contentItem)" >
#foreach (var media in Model.ContentItem.PortfolioPart.PortfolioImage.MediaParts)
{
<img src="#Display(media.MediaUrl)" />
}
<span class="type">#Display(Model.ContentItem.PortfolioPart.PortfolioCoreArea.Value)</span>
<span class="portfolio-item-content">
<span class="header">#Display(Model.ContentItem.TitlePart.Title)</span>
<span class="body">
<p>
#Display(Model.ContentItem.PortfolioPart.PortfolioTagline.Value)
</p>
</span>
</span>
</a>
</li>
The benefit with this method is that I can apply all of the values in a couple of views and it's more readable. Obviously the problem with this is that if any properties or parts are removed, the template breaks.
Is there a way in Orchard to have the best of both worlds? I can't have a wrapper or template for every field - this would end up potentially hundreds of fields by the end. I also might need to display content types in multiple places with different views - each field would then require a whole new set of wrappers or alternates for every projection.
Please let me know if I'm missing anything or if there is a better way to do this besides manually traversing to the properties I need. I need a way to be able to easily plug in properties into very specific html.
My final thought was to use very specific templates for custom content types using the object model but still provide good general templates/placement.info file so that general Orchard content is flexible but the custom content types have to stay how they are.
Side thought - I guess another option would be to wrap any code that accesses a property directly in a try catch block or some kind of error handler helper, but that doesn't seem like a "best practice".

I think the techniques in this article are what you're looking for: http://weblogs.asp.net/bleroy/archive/2013/02/13/easy-content-templates-for-orchard-take-2.aspx

Related

Kentico Widgets :: Matching fields with HTML structure of Web Part

I really hope I'm making sense with this one.
I'm trying to create a widget from a custom webpart that I created. It's nothing special at all as you can see:
<h3>Header</h3>
<p>Intro Copy</p>
<ul>
<li>List item one</li>
<li>List item two</li>
<li>List item three</li>
</ul>
I now want to be able to create a widget from this and create new fields that will be used to populate the above DOM. What do I need to do in order or do this.
In an example I saw for the demo site, they populated the bg image with:
style="background-image: url('{% ResolveUrl(PathToImage) %}');"
That was however done on the front facing part of the CMS and I'm trying to do it within the solution.
Any thoughts?
It's all in your layout or code behind. Your layout can have that code (but in ASCX format) and it will work just fine. OR you can add literal controls to the page based on the fields and what the user has entered.
Doing it in your layout is more restrictive and specific to that one application but allows you to use multiple new webpart layouts. Using the code approach allows you to be more dynamic but doesn't allow you to use the built-in layouts of the webpart/widget.
I would just use the Transformable Web Part in the Marketplace...it does exactly what you want it to do. Create a custom web part, and you use a Transformation to style the Web Part Properties into the DOM elements.
https://devnet.kentico.com/marketplace/web-parts/transformable-web-part
Reason why i built it!
I've done something similar in the past, using what I call generic web parts. I wrote a blog on it last year - it might help out with what I think you're trying to achieve:
http://www.mattnield.co.uk/Posts/Show/generic_web_parts_for_rapid_development
Why are you choosing to go with Widgets? If you want to access any field inside the transformation within the web part it's feasible by the same way as you define in inbuilt web parts.
If you want to perform any function like onload etc. then you need to use kentico API to access any data.
If you provide more insight on what is required, I can help further

Custom rendering of a taxonomy - without using HTML lists

I have a multi-layer taxonomy that I want to display in a Bootstrap 'row/column' layout. How do I stop Orchard from rendering it as an HTML list?
At present I am having to create alternates at quite a high level. So far:
1. Parts.TaxonomyPart-url-MyTaxonomy
2. TaxonomyItem-MyTaxonomy
3. TaxonomyItemLink-MyTaxonomy
The first is required to remove the main <ul> that is automatically added by Orchard.
The second removes the <ul> from any child terms
The third handles the display of the actual taxonomy term (my custom HTML).
It works, but I'm sure there's a less cluttered way of doing it that I can't see.
I would prefer to alter the rendering of the taxonomy this way rather than create a projection, as I find projections don't handle taxonomies with hierarchies very well / easily.
Any help / suggestions would be much appreciated! Comment if you need more info.

EE alternative to WordPress shortcodes?

Any way in Expression Engine to simulate Wordpress' shortcode functionality?
I want to abide by community rules, and there's a disclaimer when clicking in the "answer" section of an existing question that says I should actually ANSWER the question, not respond to other answers.
As such, I have the same question as the one above. I am a dev with roots in WordPress and I would like to mimic the behavior of WP shortcodes in Expression Engine. All I want to do is save a snippet of code as a template that can be re-used all across my site.
For example, if I want to use an accordion menu on several pages, I could just click click while editing a page and the code appears with placeholder content that the user/dev can then replace with real content). Do I need a graphic slideshow? Click click, define the images/headings/text overlays.
As I'm posting this, I'm about to scour the EE plug-ins library but since I haven't found anything before, I wanted to post here first.
I cover an approach that I've used before in http://www.tyssendesign.com.au/articles/cms/more-stash-examples/ along with a couple of other examples of using Stash.
Short answer: there is not such a thing ... yet. The Shortcode add-on is currently in beta.
Long answer for now: use custom fields. Example: a Matrix field for your accordion, with your columns defined, and add as many row as you like. Then add tags for that in your template.
Same with a Gallery - create a Gallery field (Matrix works great for this again), then add the code to your template to build the gallery.
If these fields are made optional, then they only appear on the front-end when used.
If you want to get fancy and inject these chunks of content into your main content area, you can use NSM Transplant to do so.
Here's a simplified snippet of code I use on one site to acheive this:
{exp:nsm_transplant:body}
{inline_media}
{exp:nsm_transplant:content id="media_{row_count}"}
<figure class="{alignment}">
{exp:ifelse parse="inward"}
{if image}
{if "{alignment}" == "aligncenter"}
{exp:ce_img:make src="{image:resized}" width="860" quality="80" output='<img src="{made_url}" alt="" />'}
{if:else}
{exp:ce_img:make src="{image:resized}" width="430" quality="80" output='<img src="{made_url}" alt="" />'}
{/if}
{if:elseif video}
{if "{alignment}" == "aligncenter"}
{exp:antenna url="{video}" max_width="860"}
{if:else}
{exp:antenna url="{video}" max_width="430"}
{/if}
{if:elseif gallery}
{gallery}{embed="galleries/_embed" entry_id="{entry_id}"}{/gallery}
{/if}
{if caption}<figcaption>{caption}</figcaption>{/if}
{/exp:ifelse}
</figure>
{/exp:nsm_transplant:content}
{/inline_media}
{content}
{/exp:nsm_transplant:body}
In this case authors use {media_1}, {media_2} etc, to embed photos, videos, and galleries inside the content.
Another solution you can look at is Content Elements, which allows a more freeform method of populating an entry with a single custom field.
Hope that helps!
You can also use global variables within EE templates. You cannot use EE tags inside templates, but global variables do work. So anything that you can save as a global variable (possibly including variables made with the addon Low Variables, but I have not verified that) can be included into an EE template.
So if you need static HTML, or images, or whatever, you can absolutely mimic quite a bit of shortcode functionality by creating global vars and invoking them using the ordinary {global_var_name} syntax inside an entry field. Note that EE tags inside global variables will not get parsed, though, so you cannot use this to do an end run around parsing restrictions!

Display content part in Orchard

I would like to be able to specify exactly where a ContentPart is rendered in a view.
For example, in my Content.Summary.cshtml I want to wrap my title and first image from the gallery (I'm using ZenGallery) in an anchor tag. I thought I would be able to do it like this but the gallery template is not rendered.
<a href="#Url.ItemDisplayUrl((IContent)Model)">
<h2>#Model.ContentItem.TitlePart.Title</h2>
#Display(Model.ContentItem.ZenGalleryPart)
</a>
But if I do the following then the gallery template (ZenGallery.Summary.cshtml) is shown along with all other parts.
#Display(Model.Content)
I understand that the recommended way to do this is probably using Placement.info, is that right? But this way makes more sense to me and would allow for more fine grain control of the end markup. How could I achieve the markup I'm looking for?
This should give you a pretty good start on doing precisely what you want: http://weblogs.asp.net/bleroy/archive/2011/07/31/so-you-don-t-want-to-use-placement-info.aspx

modx create layout with chunk and call with template

I'm using modx revolution. I'd like to create a chunk called layout that calls other chucks example
Head
header
nav
body
footer
then in my template do something like //open layout tag[[$layout]] [[$layout]]//close layout tag. then inside of the the open close tags append my [[*content]]. this would allow me to reuse my layout template over and over again without having to replicate it in the templates. First question, is it possible, second what kind of syntax would be needed to achieve this goal? I'm rather new to modx and know it's possible with other frameworks, so any help would be appreciated. Thanks.
Sample concept done in Apache Tapestry framework, obviously different syntax, but should give you the general idea of what I'm looking for.
components/Chunks used.
Layout
Header
Nav
Footer
Inside of layout
<html>
<t:Header/>
<t:Nav/>
<t:Body/>
<t:Footer/>
</html>
Inside of Index/Template
<t:Layout>
template body content goes here ex. [[*content]]
</t:Layout>
Hope this helps to clarify.
Your post is not very clear and I think you haven't really taken much time to read up on how MODx works before looking for help.
That aside, I think what you want to do is create different templates, structured more or less like this:
[[$header]]
[[$nav]]
<div id="content">
<h1>[[*pagetitle]]</h1>
[[*content]]
</div>
[[$footer]]
That might do for your home page, then for internal pages where the layout is a bit different you can create one or more new templates for each layout:
[[$header]]
[[$nav]]
<div id="content">
<h1>[[*pagetitle]]</h1>
[[*content]]
</div>
[[$sidebar-chunk]]
[[$footer]]
You can even show different layouts using a single template something like this:
[[$header]]
[[$nav]]
<div id="content">
<h1>[[*pagetitle]]</h1>
[[*content]]
</div>
[[*parent:is=`6`:then=`
[[$recent-articles]]
`:else=`
[[$sidebar-chunk]]
`]]
[[$footer]]
That should get you started, but you'll soon realise there are multiple ways to do everything in MODx.
You can put your [[*content]] where-ever you want, even inside another chunk, if that's what you mean.
So your [[$layout]] chunk could just be this:
[[$header]]
[[$menu]]
<div id="content">
<h1>[[*pagetitle]]</h1>
[[*content]]
</div>
If you want to make some minor changes in a chunk on a template-basis you could also do something like this in the template:
[[$layout? &customContentBits=`
<h1>[[*pagetitle]]</h1>
[[*content]]
`]]
and your layout chunk could then be something like this:
[[$header]]
[[$menu]]
<div id="content">
[[+customContentBits]]
</div>
That's a placeholder ([[+customContentBits]]) which is set by adding the &customContentBits in the chunk call.
Explained that a tad more with a different use case on my blog some time ago too: http://www.markhamstra.com/modx-blog/2010/10/introducing-element-properties-and-output-modifier/
What you are asking can absolutely be done. In fact, on my website, I even have the same template/chunk combo providing multiple layouts by passing a template variable as a chunk modifier. But anyhow, let's keep things simple.
A quick note on your question., ModX doesn't use start tags and end tags, natively. It's best to stop thinking that way. Instead just place things where you want to place them. Resource variables can go in any chunk, as each resource is unique.
Create your Chunks:
First, start with the simple ones. Create your Header, Footer, and Navigation. Next, create your Body. Inside the Body, make sure to include your [[*content]] (no... it doesn't have to go into the Template. Finally, create your Layout with the following code:
[[$header]]
[[$navigation]]
[[$body]]
[[$footer]]
Create your Template:
Your template can now be as simple as [[$layout]]. You're done.
Note
While you can do this with ModX, understand that the power of ModX is that you can have multiple templates and chunks depending on the type of content you have. Singularizing everything like this really takes away a major advantage of using the platform.
Mark Hamstra more or less gave you the answer, but just to clarify: Any snippet, chunk or output of some sort in Modx can take parameters. Chunks and snippets especially can make use of these params easily. From what i understand you want to have all your templates call [[$layout]] and nothing else.
The layout chunk in turn looks like
[[$header]]
[[$navigation]]
[[$body]]
[[$footer]]
On this you simply build and add your params, nesting them down from the top like
[[$layout? &useNavigation=`1`]]
(And continue passing the param in your layout chunk)
[[$header]]
[[+useNavigation:is=`1`:then=`[[$navigation]]`]]
[[$body]]
[[$footer]]
Another way of accomplishing the same behaviour would be to use a single template to which you have connected a series of template variables that decide how the template looks like. You might have template variable called useNavigation of checkbox type. If you check this through the resource editor it will be passed to your $layout chunk directly without having to add params into the $layout chunk call.
[[$layout]]
(Just call layout normally and add the TV checks to the layout chunk directly.
Note the difference between calling a TV and a placeholder, + vs *)
[[$header]]
[[*useNavigation:is=`1`:then=`[[$navigation]]`]]
[[$body]]
[[$footer]]

Resources