Closed bettinz closed 4 years ago
I am using webpack encore with {{ sg_datatables_render_js(datatable) }}
added at the end of the page after jquery is loaded.
And everything works properly.
Hello @Seb33300 , thank you for the reply. This is my situation:
base.html.twig
<!DOCTYPE html>
<html lang="it">
<head>
<title>Test</title>
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
{% endblock %}
{% block headJavascript %}
{% endblock %}
{% block customScript %}
{% endblock %}
{% block customCss %}
{% endblock %}
</head>
<body>
<div class="wrapper ">
{% block content %}
{% endblock %}
</div>
{% block javascripts %}
{{ encore_entry_script_tags('app') }}
{% endblock %}
{% block customJavascript %}
{% endblock %}
</body>
</html>
table.html.twig
{% extends 'base.html.twig' %}
{% block content %}
{{ sg_datatables_render_html(datatable) }}
{% endblock %}
{% block customJavascript %}
{{ sg_datatables_render_js(datatable) }}
{% endblock %}
webpack.config.js
var Encore = require('@symfony/webpack-encore');
// Manually configure the runtime environment if not already configured yet by the "encore" command.
// It's useful when you use tools that rely on webpack.config.js file.
if (!Encore.isRuntimeEnvironmentConfigured()) {
Encore.configureRuntimeEnvironment(process.env.NODE_ENV || 'dev');
}
Encore
// directory where compiled assets will be stored
.setOutputPath('public/build/')
// public path used by the web server to access the output path
.setPublicPath('/build')
// only needed for CDN's or sub-directory deploy
//.setManifestKeyPrefix('build/')
/*
* ENTRY CONFIG
*
* Add 1 entry for each "page" of your app
* (including one that's included on every page - e.g. "app")
*
* Each entry will result in one JavaScript file (e.g. app.js)
* and one CSS file (e.g. app.css) if your JavaScript imports CSS.
*/
.addEntry('app', './assets/js/app.js')
//.addEntry('page2', './assets/js/page2.js')
// When enabled, Webpack "splits" your files into smaller pieces for greater optimization.
.splitEntryChunks()
// will require an extra script tag for runtime.js
// but, you probably want this, unless you're building a single-page app
.disableSingleRuntimeChunk()
/*
* FEATURE CONFIG
*
* Enable & configure other features below. For a full
* list of features, see:
* https://symfony.com/doc/current/frontend.html#adding-more-features
*/
.cleanupOutputBeforeBuild()
.enableBuildNotifications()
.enableSourceMaps(!Encore.isProduction())
// enables hashed filenames (e.g. app.abc123.css)
.enableVersioning(Encore.isProduction())
// enables @babel/preset-env polyfills
.configureBabelPresetEnv((config) => {
config.useBuiltIns = 'usage';
config.corejs = 3;
})
// enables Sass/SCSS support
//.enableSassLoader()
.copyFiles({
from: './assets/images',
to: 'images/[path][name].[hash:8].[ext]',
})
// uncomment if you use TypeScript
//.enableTypeScriptLoader()
// uncomment to get integrity="..." attributes on your script & link tags
// requires WebpackEncoreBundle 1.4 or higher
//.enableIntegrityHashes(Encore.isProduction())
// uncomment if you're having problems with a jQuery plugin
.autoProvidejQuery()
// uncomment if you use API Platform Admin (composer req api-admin)
//.enableReactPreset()
//.addEntry('admin', './assets/js/admin.js')
;
module.exports = Encore.getWebpackConfig();
app.js
/*
* Welcome to your app's main JavaScript file!
*
* We recommend including the built version of this JavaScript file
* (and its CSS file) in your base layout (base.html.twig).
*/
// any CSS you import will output into a single css file (app.css in this case)
import '../css/app.css';
// Need jQuery? Install it with "yarn add jquery", then uncomment to import it.
import $ from 'jquery';
import 'bootstrap';
import 'popper.js';
import '@fortawesome/fontawesome-free';
import 'bootstrap-select';
import 'datatables.net';
import 'datatables.net-bs4';
package.json
{
"devDependencies": {
"@symfony/webpack-encore": "^0.30.0",
"bootstrap": "^4.5.0",
"bootstrap-select": "^1.13.17",
"core-js": "^3.0.0",
"jquery": "^3.5.1",
"popper.js": "^1.16.1",
"regenerator-runtime": "^0.13.2",
"webpack-notifier": "^1.6.0"
},
"license": "UNLICENSED",
"private": true,
"scripts": {
"dev-server": "encore dev-server",
"dev": "encore dev",
"watch": "encore dev --watch",
"build": "encore production --progress"
},
"dependencies": {
"@fortawesome/fontawesome-free": "^5.13.0",
"@ttskch/select2-bootstrap4-theme": "^1.4.0",
"add": "^2.0.6",
"bootstrap-daterangepicker": "^3.1.0",
"datatables.net": "^1.10.21",
"datatables.net-autofill-bs4": "^2.3.5",
"datatables.net-buttons-bs4": "^1.6.2",
"datatables.net-datetime": "^0.0.5",
"datatables.net-fixedcolumns-bs4": "^3.3.1",
"datatables.net-fixedheader-bs4": "^3.1.7",
"datatables.net-keytable-bs4": "^2.5.2",
"datatables.net-responsive-bs4": "^2.2.5",
"datatables.net-select-bs4": "^1.3.1",
"devbridge-autocomplete": "^1.4.11",
"highcharts": "^8.1.0",
"js-cookie": "^2.2.1",
"moment": "^2.26.0",
"select2": "^4.0.13",
"sizzle": "^2.3.5",
"vesper-icons": "^5.11.2",
"yarn": "^1.22.4"
}
}
Am I missing something? Thanks
I think I've solved with the same configuration but adding
global.$ = $;
in app.js
I'll update the issue tomorrow. Thanks for your support (and for the bundle 😃 )
After webpack introduced defer
option by default from version >= 1.9 this global.$ = $;
fix no longer works. https://github.com/symfony/webpack-encore/issues/956#issuecomment-810330909
I found an ugly solution, but it works.
First this is how you render the javascripts:
{% block javascripts %}
{{ sg_datatables_render_js(datatable) | replace({'$(document).ready(': 'let tableInit = ('}) | raw }}
{{ encore_entry_script_tags('app') }}
{% endblock %}
I'm replacing the document ready with custom function which will be used later to initiate.
// app.js
const $ = require('jquery');
const dt = require( 'datatables.net' );
require('datatables.net-bs4/js/dataTables.bootstrap4.js')
require('datatables.net-bs4/css/dataTables.bootstrap4.css')
$(function () {
tableInit();
});
Hello, I've an issue with this bundle; I'm using webpack encore and the datatable is rendered only if I use the jQuery script at the top of the page. If I place everything with webpack at the bottom, it stop working
ReferenceError: Can't find variable: $
The --> is the line with the error. I've tried disabling AMD in webpack, including
{{ sg_datatables_render_js(datatable) }}
at the very bottom of the page; no success. I've also tried with:Can you help me to understand what I'm doing wrong? Thanks