Skip to content

Rendering and options ​

Use CarveRenderer when you need Carve output in PHP outside a template.

Your first render ​

php
use MarkupCarve\CakeCarve\Carve\CarveRenderer;

$renderer = new CarveRenderer();
echo $renderer->toHtml('Hello, *CakePHP*.');
html
<p>Hello, <strong>CakePHP</strong>.</p>

Choose an output format ​

MethodReturnsUse for
toHtml($source)HTML stringWeb pages.
render($source)RenderedCarveHTML plus headings, metadata, and warnings.
toText($source)Plain text stringSearch or previews.
toMarkdown($source)Markdown stringExport.
toAnsi($source)ANSI stringTerminal display.
renderFileAs($path, $format)StringRender a file as html, text, markdown, or ansi.

Read the RenderedCarve result ​

php
$result = $renderer->render('# Hello', ['warnings' => true]);
echo $result->html();
$headings = $result->toc();
$warnings = $result->warnings();
html
<section id="Hello">
  <h1>Hello</h1>
</section>

toc() returns heading records with level, text, HTML, and anchor ID. warnings() returns parse findings when warnings is enabled. hasFrontmatter() reports whether frontmatter was found, and meta() decodes it. JSON works by default; YAML needs symfony/yaml or PHP's yaml extension.

php
$result = $renderer->render("---json\n{\"title\":\"Hello\"}\n---\n# Hello\n", [
    'extensions' => ['frontmatter'],
]);
echo $result->meta('title'); // Hello

Use presets and safe mode ​

The built-in presets are default, comment, and trusted. Use comment for visitor content. See safe mode for the trust rules.

php
echo $renderer->toHtml('Hello, *CakePHP*.', ['preset' => 'comment']);
html
<p>Hello, <strong>CakePHP</strong>.</p>

Combine options ​

The merge order is renderer defaults, app Carve config, selected preset, then per-call options. The last value for a key wins. For example:

php
$renderer = new CarveRenderer(['safeMode' => false]);
$options = $renderer->resolveOptions([
    'preset' => 'comment',
    'profile' => 'minimal',
]);
// safeMode is 'strict'; profile is 'minimal'.

Here the constructor sets safeMode to false; the comment preset changes it to strict; the call changes only profile.

Render files and includes ​

php
$result = $renderer->renderFile(ROOT . '/content/article.crv', [
    'includeRoot' => ROOT . '/content',
]);
echo $result->html();

includeRoot confines filesystem includes to that root. Use an absolute path. A custom includeResolver takes precedence. File results also expose dependencies() and suppressedWarnings(). lintFile($path, $options) uses the same resolver selection.

Format, lint, and import ​

php
$formatted = $renderer->format("Hello, *CakePHP*.\n");
$findings = $renderer->lint("Hello, *CakePHP*.\n");
$imported = $renderer->import('**bold**', 'markdown');
echo $imported->value;

lint() findings contain rule, message, line, and column. import() accepts markdown, html, djot, and bbcode; inspect diagnostics and losses() before replacing source files. See commands for file workflows.

Options reference ​

These defaults come from CarveRenderer and config/app.example.php.

OptionValuesDefaultWhat it does
safeModetrue, false, strict, SafeModetrueControls the engine's safe rendering policy.
rawHtmlnull, strip, escape, allownullHandles explicit raw HTML under safeMode: true.
xhtmlBooleanfalseEmits XHTML style empty tags.
strictBooleanfalseThrows on parse errors.
warningsBooleanfalseCollects parse warnings in the result.
profilenull, full, article, comment, minimal, ProfilenullLimits allowed markup features.
onDisallowedstrip, to_text, errorstripRemoves, converts, or rejects disallowed markup.
modeinteractive, staticinteractiveSelects interactive or build time rendering.
softBreakModenull, newline, space, brnullKeeps a source line break, turns it into a space, or emits <br>.
smartTypographynull, Boolean, SmartTypographyModenullControls typographic substitutions.
symbolsName to string map[]Replaces named symbols.
sourceLinesBooleanfalseAdds source line data to HTML.
labelsName to string map[]Replaces engine UI labels, such as footnote backlink text.
roundTripModeBooleanfalseAdds carriers for HTML or Markdown round trips.
renderersName to closure map[]Renders extension blocks at build time in static mode.
includeRootnull, pathnullSets the filesystem include boundary.
includeResolvernull, resolver object or classnullSupplies a custom include source.
extensionsNames, specs, classes, instances[]Enables extra markup features.
presetsName to option mapdefault, comment, trustedDefines reusable option sets.
presetConfigured preset namedefaultSelects an option set per call.
cachefalse, config mapfalseCaches render results with CakePHP Cache.
debugnull, BooleannullControls helper timing comments; null follows app debug.

Released under the MIT License.