<aname="Whataregridlayouts?"></a><h2>What are grid layouts?</h2>
<p>To understand how the grid layout editor works, we must <ahref="">first understand</a> the structure of the grid layouts.</p>
<p>Grid layouts consists of two main areas that need to be configured, <em>grid layout area</em> and <em>grid rows</em>.</p>
<aname="GridLayout"></a><h4>Grid Layout</h4>
<p>The <em>layout area</em> is where the overall page layout is defined.
<em>Layout areas</em> are divided in to <em>layout sections</em> e.g. a sidebar section and content section. The size of the <em>layout sections</em> is defined in columns. For a full-width content area use max number of columns (12 for Bootstrap 3). Each <em>layout section</em> contains one or more <em>rows</em>.</p>
<p>Grid <em>rows</em> is where the actual content goes. Each row is divided into <em>cells</em> that contain the property editors. The size of the cells is defined in columns. Unlike the <em>layouts sections</em> it is possible to add more <em>cells</em> than the max number of columns - they will stack as they should in a grid system.</p>
<aname="ConfiguringtheGridLayoutdatatype"></a><h2>Configuring the Grid Layout data type</h2>
<p>A grid layout contains multiple configuration options to allow developers to tailor the grid to a very specific site design.
The configuration can be divided into 4 overall parts:</p>
<aname="Layouts"></a><h3>Layouts</h3>
<p>A layout is the general grid "container", it contains one or more sections which content editors can use to insert pre-configured <strong>rows</strong>. There are 2 main usage scenarios of layouts:</p>
<ol>
<li>a single column layout which to the content editor will act like a full page canvas to insert elements on</li>
<li>a multiple column layout with a main content body, and one or more sidebar columns to insert lists or other sidebar widgets on.</li>
<p>You can however configure as many layouts and layout sections as you wish, each section in the layout must be given a width in columns, so editors gets an accurate preview of their layout.</p>
<p>It is possible to setup configurable attributes (class, rel, href) and inline styling on sections.
<p>A row in the grid editor contains one or more cells, which divide the row into areas where editors can enter content. So a row is merely a container of areas to insert content into. When you add a new row, you are asked to give it a name, then define cells inside the row by clicking the "+" icon. Each cell has a default width set to 4, but by clicking the inserted cell you can control its width.</p>
<p>It is possible to setup configurable attributes(class, rel, href) and inline styling on rows.</p>
<p>You can add as many cells as you like. If they overflow the total width of the row, they will simply be arranged after each other horizontally as you'd expect in a grid system.</p>
<p>Each cell can by default contain any type of editor such as simple textstring editors, imagespicker, embedded media or umbraco macros. To override this behavior, uncheck the <strong>allow all editors</strong> option and you can specify which editors will be available for the row. </p>
<aname="Settingsandstyling"></a><h3>Settings and styling</h3>
<p>A grid layout can also expose custom settings - such as data-attributes or styling options - on each cell or row. This allows editors to use a friendly UI to add configuration values to grid elements. When custom settings and styles are applied, they will by default be included in the grid html as either html attributes or inline styles.</p>
<p>These settings and styles must be configured by developers when setting up the grid layout data type.</p>
<aname="Configuringacustomsettingorstyle"></a><h3>Configuring a custom setting or style</h3>
<p>To add a setting, click the edit settings link. This will expand a dialog showing you the raw configuration data. This data is in the JSON format and will only save if its valid JSON.</p>
<p>The settings data could look like this, with an object for each setting:</p>
<pre><code>[
{
"label": "Class",
"description": "Set a css class",
"key": "class",
"view": "textstring",
"modifier": "col-sm-{0}",
"applyTo": "row|cell"
}
]
</code></pre>
<p>The different values are:</p>
<ul>
<li><strong>label</strong> : Field name displayed in the content editor UI</li>
<li><strong>description</strong> : Descriptive text displayed in the content editor UI to guide the user</li>
<li><strong>key</strong> : The key the entered setting value will be stored under.</li>
<li><strong>view</strong> : The editor used to enter a setting value with.</li>
<li><strong>prevalues</strong> : For views that need predefined values, e.g. the radiobuttonlist view.</li>
<li><strong>modifier (optional)</strong> : A string formater to modify the output of the editor to prepend or append extra values.</li>
<li><strong>applyTo (optional)</strong> : States whether the setting can be used on a cell or a row. If either not present or empty, the setting will be shown both on Rows and Cells.</li>
</ul>
<p><strong>label</strong> and <strong>description</strong> are straight-forward.</p>
<p><strong>key</strong> defines the alias the configuration is stored under and by default the alias of the attribute will also be the attribute on the rendered html element. In the example above any value entered in this settings editor will be rendered in the grid html as:</p>
<p>By changing the key of the setting you can modify the <code><div></code> element's attributes like <code>class</code>, <code>title</code>, <code>id</code> or custom <code>data-*</code> attributes.</p>
<p><strong>view</strong> the view defines the editor used to enter a value. By default Umbraco comes with a collection of prevalue editors:</p>
<ul>
<li>textstring</li>
<li>textarea</li>
<li>radiobuttonlist</li>
<li>mediapicker</li>
<li>imagepicker</li>
<li>boolean</li>
<li>treepicker</li>
<li>treesource</li>
<li>number</li>
<li>multivalues</li>
</ul>
<p>Alternatively you can also pass in a path to a custom view like "/app_plugins/grid/editors/view.html"</p>
<p><strong>prevalues</strong> is for views that need predefined values, e.g. the radiobuttonlist view. Prevalues are defined as strings in an array:</p>
<pre><code>"prevalues":[
"value_1",
"value_2",
"value_3"
]
</code></pre>
<p>and will translate in to three different options where each string will become a radiobutton. The strings represent the value of the options.</p>
<p><strong>modifier</strong> is a basic way to prepend, append or wrap the value from the editor in a simple string. This is especially useful when working with custom styles which often requires additional values to function. For instance if you want to set a background image you can get an image path from the image picker view. But in order for it to work with css it has to be wrapped in <code>url()</code>. In that case you set the <strong>modifier</strong> to <code>url('{0}')</code> which means that <code>{0}</code> is replaced with the editor value.</p>
<p>There are many ways to combine these, here are some samples:</p>
<p><strong>Set a background image style</strong></p>
<pre><code>{
"label": "Background image",
"description": "Choose an image",
"key": "background-image",
"view": "imagepicker",
"modifier": "url('{0}')"
}
</code></pre>
<p><strong>Set a title setting</strong></p>
<pre><code>{
"label": "Title",
"description": "Set a title on this element",
"key": "title",
"view": "textstring"
}
</code></pre>
<p><strong>Set a data-custom setting</strong></p>
<pre><code>{
"label": "Custom data",
"description": "Set the custom data on this element",
"key": "data-custom",
"view": "radiobuttonlist",
"prevalues": [
"value_1",
"value_2",
"value_3"
]
}
</code></pre>
<aname="Multiplesettingsandstyles"></a><h3>Multiple settings and styles</h3>
<p>You can add multiple settings and styles configurations on a datatype. This is done by creating a new setting or style object. Remember to seperate the objects with a comma.</p>
<p><strong>Adding multiple settings</strong></p>
<pre><code>[
{
"label": "Class",
"description": "Set a class on this element",
"key": "class",
"view": "textstring"
},
{
"label": "Title",
"description": "Set a title on this element",
"key": "title",
"view": "textstring"
},
{
"label": "Custom data",
"description": "Set the custom data on this element",
"key": "data-custom",
"view": "textstring"
}
]
</code></pre>
<aname="Fullwidthsettingsandstyles"></a><h3>Full-width settings and styles</h3>
<p>It is possible to use settings and styles to add full-width background-images, background-colors and so forth. Just make sure the surrounding <em>section</em> is full-width(12 columns by default) and the <em>rows</em> inside it will automatically become full-width.</p>
<aname="Rendergridintemplate"></a><h1>Render grid in template</h1>
<p>This will by default use the view <code>/views/partials/grid/bootstrap3.cshtml</code> you can also use the built-in bootstrap2.cshtml view by overloading the method: </p>
<p>A grid editor is the component responsible for getting data into the grid - that could be a simple text field or a media picker. They're built in the same way as a property editor thus consists of 3 parts:</p>
<ul>
<li>.html view file</li>
<li>.js controller</li>
<li>.cshtml serverside renderer</li>
</ul>
<p>The view is what the editor sees, the controller handles how it acts and the cshtml determines how the entered data is rendered in the template.</p>
<p>All editors are specified in <code>config/grid.editors.config.js</code> file which uses the json format. Foreach editor you have an object like so: </p>
<pre><code>{
"name": "Rich text editor",
"alias": "rte",
"view": "rte",
"icon": "icon-article"
}
</code></pre>
<p>The values are:</p>
<ul>
<li><strong>name</strong>: The name of the editor</li>
<li><strong>alias</strong>: Unique alias of the editor</li>
<li><strong>icon</strong>: Icon shown to the editor, uses same icon classes as the rest of </li>
<li><strong>view</strong> the view defines the editor used to enter a value. By default Umbraco will look in <code>umbraco/views/propertyeditors/grid/editors</code> for a html view to use - but you can pass in your own path</li>
<p>In this sample, the <code>config.style</code> value is applied to the editor so users can see an accurate preview in the backoffice, and <code>config.markup</code> is the string rendered on the server side.</p>
<aname="Buildyourowneditor"></a><h4>Build your own editor</h4>
<p>Create a file in <code>/app_plugins/yourpackage/editor.html</code> and add the following to the editor.html file: </p>
<p>Save the file and add an editor to the <code>grid.editors.config.js</code> file:</p>
<pre><code>{
"name": "Code",
"alias": "code",
"view": "/app_plugins/yourpackage/editor.html",
"icon": "icon-code",
"config": {
"color": "red",
"text-align": "right"
}
}
</code></pre>
<p>Add a new file: <code>/app_plugins/yourpackage/editor.cshtml</code> - this file will handle rendering the entered data - this path is done by convention so: </p>
<p>If you are building something just slightly more complex then a text area, you will need to add a controller to the grid editor view. So first add a ng-controller attribute to the grid editor html - this works just like building a property editor: </p>
<p>To wire up a controller to this view, create the file <code>/app_plugins/yourpackage/editor.controller.js</code> and add a standard angular controller declaration: </p>
<p>Finally, we need to tell umbraco load this javascript controller when the umbraco application boots, this is also just like building a property editor, so create a <code>package.manifest</code> file in the <code>/yourpackage</code> folder, and configure it to load your controller file. </p>
<pre><code>{
javascript:[
"/app_plugins/yourpackage/editor.controller.js"
]
}
</code></pre>
<p>So to summarize, to create a custom grid editor from scratch, you will need to: </p>
<ul>
<li>Create a grid editor view <code>.html</code> file</li>
<li>Create a grid render <code>.cshtml</code> file</li>
<li>Create a grid editor controller <code>.js</code> file</li>
<li>Create a <code>package.manifest</code> file to make umbraco load needed files</li>
<li>Register the editor in the <code>/config/grid.editors.js</code> file</li>
</ul>
<p>This process tries to be as close to building property editors as currently possible.</p>
Our.umbraco.org is the community mothership for Umbraco, the open source asp.net cms. With a friendly forum for all your questions, a comprehensive documentation and the a ton of packages from the community.