Implementation:Ggml org Llama cpp Jinja Value
| Knowledge Sources | |
|---|---|
| Domains | Template_Engine |
| Last Updated | 2026-02-15 00:00 GMT |
Overview
Implements the complete Jinja value type system including all primitive types, built-in functions, methods, filters, and JSON-to-value conversion used by the Jinja template engine.
Description
This file is the largest component of the Jinja engine, providing the runtime type system and standard library that makes Jinja templates functional. It implements func_args for function argument handling (positional and keyword), Python-style slice() for arrays and strings, and global_from_json() to convert nlohmann::json objects into Jinja values with optional input marking. The file contains implementations for all value type methods (array operations, string methods like split/join/replace, object access), built-in functions (range, namespace, cycler, joiner, dict), and Jinja filters (tojson, selectattr, map, reject, batch, items, pprint, etc.).
Usage
Use this module when implementing or extending the Jinja template engine's runtime behavior. All template operations ultimately resolve to method calls defined in this file, making it the central dispatch point for value manipulation in Jinja templates.
Code Reference
Source Location
- Repository: Ggml_org_Llama_cpp
- File: common/jinja/value.cpp
- Lines: 1-1322
Signature
namespace jinja {
value func_args::get_kwarg(const std::string & key, value default_val) const;
value func_args::get_kwarg_or_pos(const std::string & key, size_t pos) const;
value func_args::get_pos(size_t pos) const;
value func_args::get_pos(size_t pos, value default_val) const;
void func_args::push_back(const value & val);
void func_args::push_front(const value & val);
const std::vector<value> & func_args::get_args() const;
template<typename T>
static T slice(const T & array, int64_t start, int64_t stop, int64_t step = 1);
template<typename T_JSON>
void global_from_json(context & ctx, const T_JSON & json_obj, bool mark_input);
std::string value_to_json(const value & val, int indent, const std::string_view item_sep, const std::string_view key_sep);
}
Import
#include "runtime.h"
#include "value.h"
#include <nlohmann/json.hpp>
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| key | std::string | Yes | Keyword argument name for func_args lookups |
| pos | size_t | Yes | Positional argument index for func_args lookups |
| default_val | value | No | Default value returned when argument not found |
| json_obj | T_JSON (nlohmann::json) | Yes | JSON object to convert to Jinja values via global_from_json |
| mark_input | bool | Yes | Whether to mark strings as user input for provenance tracking |
| array | T (vector/string) | Yes | Container to slice in Python-style slice operations |
| start / stop / step | int64_t | Yes | Slice parameters following Python semantics |
Outputs
| Name | Type | Description |
|---|---|---|
| value | std::shared_ptr<value_t> | Jinja value handle wrapping the result of argument lookups or conversions |
| json_string | std::string | JSON serialization of a Jinja value via value_to_json |
| sliced | T | Sliced copy of array or string following Python slice semantics |
Usage Examples
// Retrieve a keyword argument with default
value result = args.get_kwarg("separator", mk_val<value_string>(" "));
// Get positional or keyword argument
value first = args.get_kwarg_or_pos("items", 0);
// Convert JSON to Jinja context values
jinja::context ctx;
nlohmann::json data = {{"messages", {{{"role", "user"}, {"content", "Hello"}}}}};
jinja::global_from_json(ctx, data, /* mark_input */ true);
// Convert Jinja value back to JSON string
std::string json_str = jinja::value_to_json(val, /* indent */ 2);