= MasterView - Rails-optimized (x)html friendly template engine == Recent changes MasterView now by default generates five masterview template files instead of one, though you can specify the --single-file option to only generate one file. Using one masterview template file gives the most DRY implementation but since all the views are in one file, it can become a point of contention and is a little more difficult for designers to work with. This Version 0.1.0 upgrade fixes that by generating one masterview template for each action (list, new, edit, show, destroy). The layout and message partials are defined in the list template and are imported into the other files so that you can have a complete page rendering when working with style and design of page. Similarly new defines the form partial which is imported into edit, and show defines a _show partial which is imported into destroy. One can edit the layout or other partials in the file they are defined in, so for instance to edit the layout, edit the list template and once finished, you can trigger the other templates to be rebuilt with the current information. MasterView now contains an MasterView admin controller/view to visually see all the status and details about the templates. You can rebuild any out of sync templates from this page. Alternatively you can view and rebuild templates from the command line as well using the built in rake commands (mv:list, mv:list_all, mv:rebuild TEMPLATE=foo.html, mv:rebuild_all). To make it easy to add additional actions which also import the layout, MasterView now has a way to generate a starter template file for your action by simply choosing from which other template file you want to import the layout from. For instance say you want to add an action called hello to your Product controller. Find the masterview template you want to get the layout imported from which could be (product_list.html, product_new.html, product_edit.html, ...), you can choose any of the templates that have the layout you want (it doesn't matter whether they defined the layout or simply imported it. To generate the starter template go to the MasterView Admin page http://yourserver/masterview and click copy link next to the appropriate template. On the next screen indicate the name of the action you wish to add (in this case 'hello') and click the submit button. MasterView generates a starter template file product_hello.html which imports the layout from the template file you chose and it creates some placeholder divs in the template that you can edit to add your hello view code. Alternatively there is also a rake mv:copy_layout TEMPLATE=product_list.html ACTION=hello command to allow you to do the same thing via command line. Note that you don't have to use the starter or generated files to use MasterView (you can just add MasterView attribute directives manually), but I found that once you had things started and wanted to reuse that same information, that having an automated way to get the layout imported makes it much easier and quicker to get things going. You can also change the default action div that is generated by simply creating your own file in app/views/masterview/admin/empty.rhtml which if found the copy_layout will use it, otherwise it uses the one from the gem/plugin. == Description MasterView is a ruby/rails optimized HTML/XHTML friendly template engine. It is designed to use the full power and productivity of rails including layouts, partials, and rails html helpers while still being editable/styleable in a WYSIWYG HTML editor. MasterView is distributed as a gem or a plugin. You may install it as a gem and then generate a lightweight plugin which mainly refers to the gem *or* you can simply install as a plugin which is self contained. I personally prefer installing as a gem for ease of management, however if you are running at a shared hosting environment you might not have authority to install this gem so you may install as a self contained plugin. If you are interested in the background story behind all this, it is at the end of this page. Author:: Jeff Barczewski Email:: jeff.barczewski @ gmail.com Rubyforge project:: masterview Website:: http://masterview.org License:: MIT open source license like Rails == Goals - Create/extend a template engine for rails that would be XHTML friendly and thus could be edited/styled with a WYSIWYG HTML editor even late in development without breaking template. - Keep it simple. DRY. No extra config files, simple syntax with ruby flavor. - Design it specifically for ruby and rails. Use the full power and not be limited in its capabilities over what can be done with ERb - Work nicely with layouts, partials, and rails html helpers. - Reduce complexity, work with existing rails code, no extra view logic or hashes than what is used by ERb. Scaffold generate initial templates or work from existing html prototype. - Reduce the numbers of files, simplifying editing. Define partials and layouts naturallyl right in the template, no need to go to another file. - Preview in browser without running an app. Allow for dummy data in the template so that the page can be viewed and styled independently of the application. - Performance equal to ERb == Prerequisites Requires:: No external dependencies Optional:: tidy (gem) and tidy library - if these are installed you can use tidy to cleanup html into valid xhtml for use by MasterView log4r (gem) - if this gem is installed then MasterView will use it for logging otherwise it defaults to using built in Logger. == Installation Install in one of the two following ways depending on whether you can use gems or not. === Installation using gems (if you are able to use gems) If you can use gems you may simply do the following gem install masterview_gem_pack Now your gem is installed and you can skip these steps in the future. After creating your rails directory, change directory to it and run the following to create a very lightweight plugin instance for this application mainly consisting of an init.rb file which will get loaded at runtime. This init.rb refers to the gem for everything but allows you to override any constants or setup that has been provided. See MasterView module masterview.rb for a list of the available constants. script/generate masterview_plugin After this MasterView is ready for use. Skip down to the Usage section for more details. === Installation without using gems, install as plugin script/plugin install svn://rubyforge.org/var/svn/masterview This will copy entire MasterView system into your vendor/plugin/masterview directory. You may tweak its init.rb to override any MasterView constants at runtime. See MasterView module masterview.rb for a list of available constants. Note that if you don't have svn (subversion) installed, you may also retrieve the plugin package (masterview_plugin.tgz or masterview_plugin.zip) from http://rubyforge.org/projects/masterview and simply extract into vendor/plugins/masterview == Usage You may add MasterView attributes to existing (x)html or you may use the masterview generator to create a complete working application. The generator can create controllers, models, and the MasterView template file similar to how the built-in generator works. Simply change directory to your rails application and run the following script/generate masterview YourModelName [YourControllerName] [--style [cssStylesheet]] [--single-file] [--show-all | --show-only list] The generator by default will generate five masterview template files, one for each distinct page, list, new, edit, show, and destroy. They exist in the app/views/masterview directory with the filename controller_action.html. The layout and message partial are defined in the list template file and imported into the others. Similarly the new template defines the form partial which is imported into edit, and finally the show file defines a _show partial which is imported into destroy. Thus there is one definition of each part (layout and partial) and they are imported into the other files where needed to provide accurate WYSIWYG design time editing. By generating separate files, teams can work on a project easier, however MasterView also supports generating all parts to a single file for the ultimate in DRY. By adding the --single-file switch MasterView will create a single file and to make it easier to use this template at design time, some design time css stylesheets are included in the file to hide all sections except one. By default the NEW section is the only one shown. Other options are --show-all which makes all sections visible or [--show-only list] which shows only the LIST section. When you are editing the masterview file you may comment/uncomment one of the other css files to show a different section. The --style param allows you to suppress default style generation and specify an existing stylesheet to use, if you exlude the stylesheet none will be used, if you include this option multiple times with different stylesheets each will be used. Once it is done generating, the generated MasterView template file will be created in app/views/masterview/controller.html. This file is html and can be edited with any standard html editor. The rails specific logic is contained in simple attributes which are ignored by html editors. The syntax for these attributes is heavily derived from the rails helper tags themselves so it should feel natural to the rails developer. Another interesting thing to know is that while all of the pages for this Model have been bundled up into a few html file for ease of editing, at runtime these templates gets rendered into the exact same layouts and partials that you would use if you were building from scratch. Its jsut that now you can see what your pages will render like in your wysiwyg html editor and change and layout accordingly. Additionally MasterView supplies some javascript to show only one action view at time (list, new, show, edit, destroy) so you can view in your browser without running in Rails. Dummy html can be included to improve the accuracy of the page which can be easily removed at runtime. To make it easier to work with in an editor, design time stylesheets are included in the file to allow you to hide all sections except the one you are working on, simply uncomment the appropriate stylesheet for the section you would like to work with. MasterView is designed to be easy for a developer and designer to work together. By keeping the template in an html friendly format, designers can apply style, layout changes, wording changes, without causing havoc on the rails view code. The designer can be involved at anytime during the development cycle including being able to change style and layout after the system is built. This is great for allowing design or wording changes without reinvolving the developers. One can even start from a designer created prototype and add MasterView tags to make it become real. Whichever way you prefer to work, MasterView accomodates you. With Version 0.1.0 a MasterView admin controller/view was added to provide a birds-eye view of all your masterview templates, the status of those templates (OK, Invalid xhtml, Conflicts, Imports outdated), the details regarding the status, and the ultimate erb files generated from each template. By default the MasterView admin controller is enabled by the plugin and is available at http://yourserver/masterview This controller can easily be disabled if not wanted by setting the EnableMasterViewAdminPages = false in the vendor/plugins/masterview/init.rb file. Additionally all of the power of the MasterView admin controller is available via a set of rake commands as well. rake mv:list mv:list_all mv:rebuild mv:rebuild_all mv:copy_layout. rake -T will give you further information about these commands. Since it can be difficult to import a layout by hand into a new file, the MasterView admin controller has a link where you can copy the layout into a new file by providing the new files Action. It will take the layout from the template chosen and create a new file controller_action.html with the layout imported and a shell for the new Action code. You can also do this from command line using rake mv:copy_layout TEMPLATE=foo_list.html ACTION=newaction command.