Dfr
Dfr

Reputation: 4185

Slim templates, custom DOCTYPE

i have a page with this doctype:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">

and converting it now to slim template format, bu slim has no such doctype in presets, and i suspect there should be way to specify custom one. Now i use |:

|<!DOCTYPE html PUBLIC "...skip...>
html
 ....

But it rendered without line break symbol after doctype line:

<!DOCTYPE html PUBLIC "...skip...><html ...

Which is undesired, any way to put them on separate lines ?

Upvotes: 3

Views: 1280

Answers (2)

matt
matt

Reputation: 79813

You don’t need the | character, Slim allows inline HTML for directly including HTML, and that includes doctypes. This doesn’t help with the formatting though, there is still no line break after it.

The doctypes are actually managed in Temple, which Slim uses. You could change this to add a custom doctype to get the behaviour you want. This does involve messing with Temple’s internal data so you need to be a little careful, especially when upgrading, as the internals may have changed.

The hash in question is has been frozen, so you can’t just add a new entry to it. You can however dup it, add an entry to the duplicated hash, and assign the new hash back to the constant. This will cause Ruby to generate a warning as you shouldn’t normally reassign constants this way.

new_hash = Temple::HTML::Fast::XHTML_DOCTYPES.dup
new_hash['rdf'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">'
Temple::HTML::Fast::XHTML_DOCTYPES = new_hash

Now you can use rdf as if it was a predefined doctype in Slim.

doctype rdf
html

This produces:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html></html>

If you want to avoid seeing the warning, set $VERBOSE to nil before changing the constant (make sure you change it back afterwards).

begin
  old_verbose, $VERBOSE = $VERBOSE, nil
  Temple::HTML::Fast::XHTML_DOCTYPES = new_hash
ensure
  $VERBOSE = old_verbose
end

(You could turn this into a method that accepts a block. If you’re using Rails it’s already been done for you with silence_warnings.)

Upvotes: 2

marcus erronius
marcus erronius

Reputation: 3703

To insert a line break at any point in the template, just add this line:

= "\n"

The "=" command in Slim executes the given Ruby code and appends it to the buffer. In this case, the Ruby code is simply a newline character.

Upvotes: 2

Related Questions