Database templates

Dreceres ràpides: navegació, cerca

Aquesta pàgina forma part de la documentació de Moodle en català, tot i que no ha estat traduïda encara. Podeu contribuir obertament a les tasques de traducció. Podeu consultar la Guia d'edició de la documentació i també participar ens els debats del fòrum de traductors de la documentació a

Templates for the Database activity module allow you to control the visual layout of information when listing, viewing or editing database entries. It is a similar to the technique used to mail merge letters in word processors such as Open Office Writer or Microsoft Word.

Tag usage

The content of each field you create for your database and some special tags (listed below) can be inserted into the output template by the use of tags.

Fields have the format
. All other tags have the format
. To use the tags in the box on the left of the page, use the HTML viewer, place your cursor in the text area of your target edit and then click on the tag you want to place. Alternatively, you may simply type the appropriate name within the required symbols like
, respectively.
  • ##edit##
    creates a clickable icon link that allows you to edit the current entry (only appears if you have the rights to do this)
  • ##delete##
    creates a link that lets you delete the current entry (only appears if you have the rights to do this)
  • ##approve##
    create a link that lets you approve the current database entry (only appears if you have the rights to do this)
  • ##more##
    creates a link to the single view, which may contain more detailed info
  • ##moreurl##
    creates just the URL for the above link, useful for creating your own links. You can click on the link icon and type
    into URL field or in source view type
    <a href="##moreurl##">[[fieldname]]</a>
  • ##comments##
    creates a link to the view/edit comments page, the link text is the current number of comments (only appears if comments are turned on)
  • ##user##
    creates a link to the user page of the user who submitted the entry, link text is their name
  • ##timeadded##
  • ##timemodified##

List template

This template allows you to control the fields used and their layout when viewing multiple entries at once (e.g. search results). It is possible that this view may simply provide an overview with more detailed information available by clicking on an entry to access the single view of the entry.

See Designing a list view in Moodle database activity for instructions on how to create a list template table.

The list template can also be used as a way to export your database as a CSV file.

Single template

This is used to display a single entry at a time and so has more space for display and can use, for example, larger versions of images or optionally provide more information than shown in the list view.


Advanced search template

An advanced search template is for creating the interface form used in the advanced search.

Add template

This template creates the interface form used when adding or editing database entries.

RSS template

Lets you control the content of the RSS feed for database entries.

CSS template

If any of the HTML in your other templates requires CSS to provide visual style you can specify it here.

Javascript template

You can use javascript to manipulate the way elements are displayed in either the List, Single or Add templates. You will need to make sure that the components you intend to manipulate are identifiable through some kind of CSS selector. This could be a class, an attribute, or an id. You should also wrap the entire template in an identifiable component.

Let's say, for example, you have a field in your database that stores a person's name and when you display the names in the List View you want to count the times a name matches some criteria and display the result.

Your database will contain a field which we will call "name". In your List template you will be able to display the contents of that field by using the [[name]] construct at the place where you want that information displayed. For example in the Repeated entry on the list template you will have:

<div class="defaulttemplate">
  <table class="mod-data-default-template">
      <tr class="r0">
        <td class="template-field cell c0">name:</td>
        <td class="template-token cell c1 lastcol">[[name]]</td>
      <tr class="r1 lastrow">
        <td class="controls template-field cell c0 lastcol" colspan="2">##edit## ##delete## ##approve## ##export##</td>

You now need to modify that entry to ensure that the part you want to manipulate is a named element.

<div class="defaulttemplate">
  <table class="mod-data-default-template">
      <tr class="r0">
        <td class="template-field cell c0 name">name:</td>
        <td class="template-token cell c1 lastcol">[[name]]</td>
      <tr class="r1 lastrow">
        <td class="controls template-field cell c0 lastcol" colspan="2">##edit## ##delete## ##approve## ##export##</td>
      <tr class="r0 summary">
        <td class="controls template-field cell c0 lastcol result" colspan="2"></td>

The footer of your list view can then contain another named element to display the result.

  <div class='result'></div>

In your JavaScript template, you can write any JS you like. We recommend writing your JS within a closure, and encourage you to make use of YUI. An example is given below:

(function() {
  var myvariable = 0,
        myothervariable = 0,
        count = 0;
  Y.all('.defaulttemplate td.countme', function() {
    var thiscount = parseInt(this.get('innerHTML'), 10);
    count += isNaN(thiscount) ? 0 : thiscount;
  Y.all('.defaulttemplate td.somecell a', function() {
    this.setAttribute('target', '_blank')
  });'.defaulttemplate td.result').set('innerHTML', count);

This will display a table of names as is usual in the list view. Now at the bottom there will also be the count of the names that matched foo or Foo.

Reset templates button

When you first create a database the templates will be pre-filled with appropriate HTML. If you later add fields then you can press the reset templates button and it will add HTML for the new fields in a similar fashion. If you have edited any of the templates in the meantime then your changes will be lost. It is recommended that you finalize the database fields before changing the template code.

See also

Using Moodle forum discussions: