Templates are the main component in the output rendering process of YARD, which is invoked when conventional HTML/text output needs to be rendered for a set of code objects.
The general design attempts to be as abstracted from actual content and templates as possible. Unlike RDoc which uses one file to describe the entire template, YARD splits up the rendering of code objects into small components, allowing template modification for smaller subsets of a full template without having to duplicate the entire template itself. This is necessary because of YARD's support for plugins. YARD is designed for extensibility by external plugins, and because of this, no one plugin can be responsible for the entire template because no one plugin knows about the other plugins being used. For instance, if an RSpec plugin was added to support and document specifications in class templates, this information would need to be transparently added to the template to work in conjunction with any other plugin that performed similar template modifications. The design goals can be summarized as follows:
- Output should be able to be rendered for any arbitrary format with little modification to YARD's source code. The addition of extra templates should be sufficient.
- The output rendered for an object should independently rendered data from arbitrary sources. These independent components are called "sections".
- Sections should be able to be inserted into any object without affecting any existing sections in the document. This allows for easy modification of templates by plugins.
Template modules are the objects used to orchestrate the design goals listed above. Specifically, they organize the sections and render the template contents depending on the format.
The Engine class orchestrates the creation and rendering of Template modules and
handles serialization or specific rendering scenarios (like HTML). To create
a template, use the
#generate, is used
specially to generate HTML documentation and copy over assets that may be
needed. For instance, an object may be rendered with:
::::.(:object => myobject)
A set of objects may be rendered into HTML documentation by using:
# all_objects is an array of module and class objects # options includes a :serializer key to copy output to the file system ::::.(all_objects, )
Note that these methods should not be called directly. The
generate method is a special kind of render
and is called from the command line utility.
A template keeps state when it is rendering output. This state is kept in
an options hash which is initially passed to it during instantiation. Some
default options set the template style (
:template), the output format (
and the serializer to use (
:serializer). This options hash is modifiable
from all methods seen above. For example, initializing a template to output as
HTML instead of text can be done as follows:
myobject.format(:format => :html)
This class abstracts the logic involved in deciding how to serialize data to the expected endpoint. For instance, there is both aand class for outputting to console or to a file respectively. When endpoints with locations are used (like files or URLs), the serializer implements the method. This allows the translation from a code object to its path at the endpoint, which enables inter-document linking.
Rendered objects are automatically serialized using the object if present, otherwise the rendered object is returned as a string to its parent. Nested Templates automatically set the serializer to nil so that they return as a String to their parent.
Creating a Template
Templates are represented by a directory inside theon disk. A standard template directory looks like the following tree:
(Assuming templates/ is a template path) templates `-- default |-- class | |-- dot | | |-- setup.rb | | `-- superklass.erb | |-- html | | |-- constructor_details.erb | | |-- setup.rb | | `-- subclasses.erb | |-- setup.rb | `-- text | |-- setup.rb | `-- subclasses.erb |-- docstring | |-- html | | |-- abstract.erb | | |-- deprecated.erb | | |-- index.erb | | `-- text.erb | |-- setup.rb | `-- text | |-- abstract.erb | |-- deprecated.erb | |-- index.erb | `-- text.erb
default refers to the template style (:template key in options hash)
and the directories at the next level (such as
class) refer to template
:type (options hash key) for a template. The next directory refers to the
output format being used defined by the
:format template option.
As we saw in the above example, the format option can be set to
would use the
html/ directory instead of
text/. Finally, the individual .erb
files are the sections that make up the template.
Note that the subdirectory
html/ is also its own "template" that inherits
from the parent directory. We will see more on this later.
Every template should have at least one
setup.rb file that defines the
method to set the
used by the template. If
a setup.rb is not defined in the template itself, there should be a template
that is inherited (via parent directory or explcitly) that sets the sections
on a newly created template.
A standard setup.rb file looks like:
def init sections :section1, :section2, :section3 end
Sections are smaller components that correlate to template fragments. Practically speaking, a section can either be a template fragment (a conventional .erb file or other supported templating language), a method (which returns a String) or another(which in turn has its own list of sections).
Sections often require the ability to encapsulate a set of sub-sections in markup (HTML, for instance). Rather than use heavier Template subclass objects, a more lightweight solution is to nest a set of sub-sections as a list that follows a section, for example:
def init sections :header, [:section_a, :section_b] end
The above example nests
section_b within the
Practically speaking, these sections can be placed in the result by
to them. A sample header.erb template might contain:
<h2>Header</h2> <div id="contents"> <%= yieldall %> </div>
This template code would place the output of
the above div element. Using
yieldall, we can also change the object that is being
rendered. For example, we may want to yield the first method of the class.
We can do this like so:
<h2>First method</h2> <%= yieldall :object => object.meths.first %>
This would run the nested sections for the method object instead of the class.
yieldall yields to all subsections, whereas
yield will yield
to each individually (in order) until there are no more left to yield to.
In the vast majority of cases, you'd want to use
makes it hard for users to override your template.
Parent directory templates are automatically inherited (or mixed in, to be more accurate) by the current template. This means that the 'default/class/html' template automatically inherits from 'default/class'. This also means that anything defined in 'default/class/setup.rb' can be overridden by 'default/class/html/setup.rb'.
Since the Template module is a module, and not a class, they can be mixed in explicitly (via include/extend) from other templates, which allows templates to share erb files or helper logic. The 'default/class' template explicitly mixes in the 'default/module' template, since it uses much of the same sections. This is done with the helpermethod, which is simply a shorthand for . It can then override (using standard inheritance) the sections from the module template and insert sections pertaining to classes. This is one of the design goals described above.
For instance, the first line in
This includes the 'default/module/html', which means it also includes 'default/module' by extension. This allows class to make use of any of module's erb files.
Inserting and Traversing Sections
The ability to insert sections was mentioned above. The class template, for instance, will modify the #init method to insert class specific sections:
def init super sections.place(:subclasses).before(:children) sections.delete(:children) sections.place([:constructor_details, [T('method_details')]]).before(:methodmissing) end
Observe how sections has been modified after the super method was called (the
super method would have been defined in
sections object is of the class and allows sections to be inserted
before or after another section using by it's given name rather
than index. This allows the overriding of templates in a way that does not
depend on where the section is located (since it may have been overriden by
You can also use
sections[:name] to find the first child section named
For instance, with the following sections declaration:
sections :a, [:b, :c, [:d]]
You can get to the :d section with:
You can use this to insert a section inside a nested set without using indexed
access. The following command would result in
[:a, [:b, :c, [:d, :e]]]:
There are also two methods,and , which allow you to insert sections before or after the first matching section name recursively. The above example could simply be rewritten as:
Overriding Templates by Registering a Template Path
Inheriting templates explicitly is useful when creating a customized template that wants to take advantage of code re-use. However, most users who want to customize YARD templates will want to override existing behaviour without creating a template from scratch.
YARD solves this problem by allowing other template paths to be registered. Because template modules are represented by a relative path such as 'default/class', they can be found within any of the registered template paths. A new template path is registered as:
At this point, any time the 'default/class' template is loaded, the template will first be looked for inside the newly registered template path. If found, it will be used as the template module, with the modules from the other template paths implicitly mixed in.
Therefore, by using the same directory structure as a builtin YARD template, a user can customize or override individual templates as if the old ones were inherited. A real world example would further modify the 'default/class' template seen above by creating such a path in our '/path/to/mytemplates' custom template path:
/path/to/mytemplates/: |-- class | |-- html | | |-- customsection.erb | |-- setup.rb
setup.rb file would look like:
def init super sections.push :customsection end
Now, when a class object is formatted as HTML, our customsection.erb will be appended to the rendered data.