Views and components | laravel-markdown-rust | Spatie       

 SPATIE  

laravel-markdown-rust
=====================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-markdown-rust](https://spatie.be/docs/laravel-markdown-rust/v1)  Views and components

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/laravel-markdown-rust/v1) 

  Views and components    
- [ Introduction ](https://spatie.be/docs/laravel-markdown-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-markdown-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/laravel-markdown-rust/v1/installation-and-setup)
- [ Usage ](https://spatie.be/docs/laravel-markdown-rust/v1/usage)
- [ Extensions and front matter ](https://spatie.be/docs/laravel-markdown-rust/v1/extensions)
- [ Testing ](https://spatie.be/docs/laravel-markdown-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/laravel-markdown-rust/v1/changelog)
- [ Views and components ](https://spatie.be/docs/laravel-markdown-rust/v1/views)

 Views and components
====================

###  On this page 

1. [ Template function and filter ](#content-template-function-and-filter)
2. [ Cached wrapper component ](#content-cached-wrapper-component)
3. [ Markdown view files ](#content-markdown-view-files)

- Enable the `views` Cargo feature.
- Integration uses the framework's own `ViewConfig`, MiniJinja engine, `Attributes` and `Html` types.

Template function and filter
--------------------------------------------------------------------------------------------------------------------------------------------

Register a configured renderer explicitly:

```
use laravel::{Application, config::Env};
use spatie_markdown::{MarkdownRenderer, views};

let app = Application::configure()
    .replace_env(Env::default())
    .with_views(|config, _| {
        views::register(config, MarkdownRenderer::new().github_flavored(true));
    })
    .create();
```

Templates can use an argument, a filter, or a block:

```
{{ markdown(source) }}
{{ source | markdown }}
{% filter markdown %}
# My title

This is a [link to our website](https://spatie.be).
{% endfilter %}
```

- The function and filter return safe template values, so generated tags are not escaped again.
- The renderer still escapes untrusted source HTML by default.
- These synchronous helpers do not call the async cache. Prepare a component before rendering the view when you need caching.

Cached wrapper component
--------------------------------------------------------------------------------------------------------------------------------

```
use laravel::{Application, config::Env, view::Attributes};
use spatie_markdown::{MarkdownConfig, views};

let app = Application::configure().replace_env(Env::default()).create();
app.scope(async || -> laravel::Result<()> {
    let mut attributes = Attributes::new();
    attributes.insert("class", "prose")?;
    attributes.insert("data-title", "Title & more")?;

    let config = MarkdownConfig::default();
    let markup = views::component("# My title", &config, attributes).await?;
    assert!(markup.into_string().contains("<div class=\"prose\" data-title=\"Title &amp; more\">"));
    Ok(())
}).await?;
```

- Pass the result into a view with `view("post", ()).with_html("content", markup)`, then print `{{ content }}`.
- The component uses the config's cache store, TTL and renderer. Disabling caching or setting zero TTL bypasses store resolution.
- The wrapper uses native attribute-name validation and value escaping. `Html` attributes and event-handler attributes are trusted application markup; never copy arbitrary user-supplied attribute names into them.
- Clone `MarkdownConfig` and replace its renderer for per-component options. Highlighting, theme, anchors, parser options and extensions all affect the cache key.

Markdown view files
-----------------------------------------------------------------------------------------------------------------

- Register `views.engine("md", views::MarkdownEngine(renderer))` in `with_views` to render `.md` views, including embedded namespaces.
- The engine reads through `Template::source`, not filesystem paths.
- Markdown files do not interpolate template expressions or support template blocks. A request for a block returns an error.
- The engine renders synchronously without caching.

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
