🎨 Customizing Templates
GitHub Project PHP uses Blade templates to generate comments for different field types. This guide explains how to customize these templates to match your needs.
Template Locations
All templates are located in the resources/views/vendor/github-project/md/
directory after publishing:
resources/views/vendor/github-project/md/
├── comment.blade.php # Main comment template
├── shared/
│ ├── author.blade.php # Author information
│ └── content.blade.php # Content rendering
└── field_types/ # Field type specific templates
├── text.blade.php
├── number.blade.php
├── date.blade.php
└── ...
Publishing Templates
To customize the templates, publish them to your application:
php artisan vendor:publish --provider="CSlant\GitHubProject\GithubProjectServiceProvider" --tag="views"
This will copy the templates to your resources/views/vendor/github-project/md/
directory.
Available Templates
Main Comment Template (comment.blade.php
)
The main template that wraps all comments. It includes:
- Project information
- Action type (created, updated, deleted)
- Content section for field changes
- Author information
Field Type Templates
Each field type has its own template in the field_types
directory. The following field types are supported:
text.blade.php
- Simple text fieldsnumber.blade.php
- Numeric fieldsdate.blade.php
- Date fieldssingle_select.blade.php
- Single-select dropdownsmulti_select.blade.php
- Multi-select fieldscheckbox.blade.php
- Boolean/toggle fieldstextarea.blade.php
- Long text contentiteration.blade.php
- Iteration/sprint fieldsmilestone.blade.php
- Milestone fieldsunsupported.blade.php
- Fallback for unsupported field types
Template Variables
Available in All Templates
[
'payload' => [
'action' => 'created|edited|deleted',
'projects_v2_item' => [
'content_type' => 'Issue|PullRequest',
'content_node_id' => 'string',
'content_title' => 'string',
'content_url' => 'string',
'project_title' => 'string',
'project_url' => 'string',
],
'sender' => [
'login' => 'string',
'html_url' => 'string',
'avatar_url' => 'string',
],
'changes' => [
'field_value' => [
'field_type' => 'text|number|date|...',
'from' => 'mixed',
'to' => 'mixed',
],
'updated_at' => 'datetime',
],
],
'fieldName' => 'string', // Current field name
'fieldType' => 'string', // Current field type
'fromValue' => 'mixed', // Previous value
'toValue' => 'mixed', // New value
'fieldData' => [ // Raw field data
'from' => 'mixed',
'to' => 'mixed',
'field' => [
'name' => 'string',
'type' => 'string',
],
],
]
Customizing a Field Type Template
To customize a field type template, copy it from the package to your application and modify it. For example, to customize the text field template:
-
Publish the templates (if not already done):
php artisan vendor:publish --provider="CSlant\GitHubProject\GithubProjectServiceProvider" --tag="views"
-
Edit the template at
resources/views/vendor/github-project/md/field_types/text.blade.php
Adding a New Field Type
To add support for a new field type:
- Create a new template file in
resources/views/vendor/github-project/md/field_types/
- Name it
{field_type}.blade.php
(e.g.,custom_field.blade.php
) - The template will automatically be used when a field of that type is encountered
Helper Functions
The following helper functions are available in templates:
color_value_format($value, $color = null)
- Formats a value with an optional colorformat_date($date, $format = 'Y-m-d H:i:s')
- Formats a date stringmarkdown_to_html($markdown)
- Converts markdown to HTML (if needed)
Example: Custom Text Field Template
{{-- resources/views/vendor/github-project/md/field_types/text.blade.php --}}
@if($fromValue != null && $toValue != null)
**`{{ $fieldName }}`** was changed from `{{ $fromValue }}` to `{{ $toValue }}`
@elseif($toValue == null)
**`{{ $fieldName }}`** was removed
@else
**`{{ $fieldName }}`** was set to `{{ $toValue }}`
@endif
Best Practices
- Keep templates simple and focused on a single responsibility
- Use GitHub Flavored Markdown for formatting
- Handle null/empty values gracefully
- Keep the output concise but informative
- Test your templates with different field values
Testing Your Templates
To test your templates, you can:
- Make changes to a project item in GitHub and check the generated comment
- Use the
tinker
console to manually trigger comment generation - Write tests that verify the template output