Theme
Version
GitHub PyPI Discord
On this page

Place JavaScript and CSS

When a page renders, Citry collects the JavaScript and CSS that its components need. By default it puts the CSS at the end of <head> and the JavaScript at the end of <body>, which suits most pages.

Read on when you need the tags somewhere else, or when the HTML is not a whole page: an email, a piece of HTML that another system places, or a page whose scripts you load yourself.

<c-css /> and <c-js />

Put <c-css /> where the CSS should go and <c-js /> where the JavaScript should go:

from citry import Component

class Page(Component):
    template = """
      <html>
        <head>
          <c-css />
          <link rel="stylesheet" href="/static/overrides.css">
        </head>
        <body>
          <c-Chart c-points="[1, 2, 3]" />
          <c-js />
        </body>
      </html>
    """

Here the component styles come before overrides.css, so the overrides win. Each tag takes no attributes and no content.

Without these tags, Citry inserts CSS before the first </head> and JavaScript before the last </body>. When the HTML has no </head> or </body>, CSS goes at the start of the output and JavaScript at the end.

Choose a strategy

The dependency strategy decides which tags Citry inserts. str(component) uses "document". To choose another, render first and pass deps_strategy to serialize():

page = Page()
rendered = page.render()
html = rendered.serialize(deps_strategy="simple")

DepsStrategy accepts four values:

  • "document" inserts every tag the page needs. When a component needs Citry's browser runtime (the JavaScript that runs Vue and server events), it adds that too.
  • "simple" inserts the components' JavaScript and CSS and their dependency files, but not the browser runtime. Use it for output with no Citry browser behavior: $component(), server events, and js_data() do not work, while css_data() does, because it is ordinary CSS.
  • "fragment" is for HTML that you insert into a page that is already open. See HTML fragments.
  • "ignore" inserts no tags. Use it when something else adds every file the components need.

With "ignore", the HTML can look right in tests but have no styles or browser behavior if nothing else adds the files.

Placement options

Use deps_position when the output is not a whole HTML page and the code that receives it decides where it goes. It works with the "document" and "simple" strategies:

page = Page()
html = page.render().serialize(
    deps_strategy="document",
    deps_position="append",
)

DepsPosition accepts three values:

  • "smart", the default, uses <c-css /> and <c-js />, or the locations described in <c-css /> and <c-js />;
  • "prepend" puts all the tags before the HTML;
  • "append" puts all the tags after the HTML.

A page that repeats a placement tag

When the page has more than one CSS or JavaScript placement tag, only the first of each kind receives the tags. The others insert nothing.

Next steps